Skip to main content
Department of Information Technology

Computer Architecture, First Course

Assembly 1

This assignment is mandatory and must be satisfactory solved to get credits for the course. See further information on the Examination page for deadlines etc.

Before You Begin!

We strongly recommend to start with the following "Getting Started" exercises:

Examination

All source code must be well commented and well structured. There will be an oral examination/presentation of the assignment to the teacher(s). All participants in a group (one or two students) must be able to explain how the program works and how parameters are transferred from the main function to the subroutines. You should also be able to explain how the register windows and the stack work. Bring printout of your code to the examination, and make sure there is a working, compiled/assembled, version of your solution in your home account on the department computer system. In case two students work together, both must be able to demonstrate and run the program.

NOTE: You will also get questions about general assembly programming at the examination. For example: What happens with the local registers when the CPU performs a save operation?

Part I: Adding numbers

Purpose

The purpose of this assignment is to gain understanding of how the stack works and is maintained on the SPARC, and how to write subroutines and manage register windows.

Background

It is common to transfer parameters to functions and procedures using the computer registers and stack. Most computer architectures have a policy how to do this. Compilers generate code according to this policy and assembly code should be written to comply with this policy, since it simplifies linking between different modules and libraries. It also makes the code easier to read and understand. In this assignment, both register and stack transfer of parameters will be explored.

NOTE: The subroutines must be self-contained and must not jump into other code. They must be called by the call operation and return according to the SPARC convention.

Assignment

Write a subroutine that adds integers. The subroutine takes as its parameter the number of numbers to add, and the numbers to add. The subroutine should return the sum of the numbers. Then write a main function that calls the add function.

An example program may begin like this. At the label antal, the total number of integers to add is stored. At the label tal, the array of integers is stored and at the label resultat, the result should be written before the main function returns. Observe that the subroutine must not reference any of these labels directly.

          .data

antal:    .word 10
tal:      .word 1,2,3,4,5,6,7,8,9,10

resultat: .skip 4

          .text
          .global main
main:

Make two versions of the program:

  1. In the first version, the subroutine must use call by value, i.e., a copy of each number must be transferred to the subroutine. The amount of numbers to add must be larger than the amount of registers available to transfer parameters, so the stack must be used. The SPARC calling convention must be followed. Also, write a main function that calls the subroutine.
  2. In the second version of the program, transfer the number of integers and a pointer to the subroutine as parameters, i.e., only two parameters. Note that this is not call by value. What is it? Put the two parameters in registers.

Part II: A Calculator

Purpose

The assignments purpose is to practice assembly programming and give an introduction in using system/library calls for I/O.

Assignment

Write a simple calculator. The program should ask the user for a string of the form integer+integer, parse the string and check syntax, perform the addition, and print out the result. Your program does not need to handle integer overflow, but must be able to detect any kind of syntax error (see below for the example run).

An important part of the task is to parse the string and convert it to and from ASCII. Here, you can reuse some of the work from exercise 2.

The program must be well structured, function calls must follow the SPARC calling convention, and you must have at least two subroutines in your program. The subroutines must perform well defined tasks, for example integer to ASCII conversion, and must not access global variables. You may (should in case of strings) use call by reference.

Write a comment in the source code for each subroutine that describes its function, and in and out parameters.

You should write this program in two versions: one which runs on the simulator, and one which runs on a real SPARC workstation/server. If you are working on a Windows-PC it is enough to try it out in the simulator.

Example runs of the simple calculator

The program must handle all these cases correctly.

Example 1

Give addition:
589+485
The sum is: 589+485=1074
Do you want to make another addition? (y/n)

Example 2

Give addition:
10000+4
The sum is: 10000+4=10004
Do you want to make another addition? (y/n)

Example 3

Give addition:
hej+hopp
Error!
Do you want to make another addition? (y/n)

Example 4

Give addition:
+563
Error!
Do you want to make another addition? (y/n)

Example 5

Give an addition:
123+
Error!
Do you want to make another addition? (y/n)

Updated  2003-11-07 16:30:20 by Dan Wallin.