$30
Minimal Submitted Files
You are required to turn in the following source file:
assignment1c.s
Objectives:
This assignment is for you to become familiar with the QTSpim simulator that we will be using in this course. You can use a machine in BYENG 214 (a computer lab) or install the QTSpim into your own computer, by downloading from:
http://sourceforge.net/projects/spimsimulator/files/
(http://sourceforge.net/projects/spimsimulator/files/)
You will edit an assembly language program, load it in the QTSpim, execute it, and step through the program.
Important Note: Your submitted assignment will be tested using SPIM, therefore you should test your program using SPIM before its submission.
---------------------------------------
Assignment Description:
1. Load the QTSpim program. You can also use other SPIM and you will have a similar, but slightly different interface. You will see the windows looking as follows:
If the console window is not visible, then select Window -> Console from the menu in the QTSpim. The console window should be empty.
The QTSpim window is divided into some sections. The left section under
Int Regs is the registers section. Registers are storage areas internal to the CPU. They are identified by notations such as $r0-$r31. In QTSpim, registers are 32-bits wide, meaning that they can store a 32-bit value. (Note that there are Int Regs to store integers, and FP Regs to store floating point numbers. We will discuss floating point numbers later in the semester.)
The right section under the tab Text is the text segment. This is where the assembly/machine language code you are executing is displayed. On the left hand side you see values such as [00400000]. These are memory addresses. The simulator begins executing code at 00400000 by default. The next column is the 32-bit machine language instruction stored at that address. It is displayed in hex. The next column is the assembly language statement that corresponds to that 32-bit machine language instruction. The next column (with the ;) is an alternative representation of the statement in column three.
The right section under the tab Data is the data segment. Any data used by your program is stored here. The first column displays a memory address (or range) and the columns display the data stored there.
2. Minimize the QTSpim window. Then using your favorite text editor (such as NotePad, TextPad, note that TextEdit for Mac is not a good choice since it deals with non-ascii characters, you will need a simple text editor where you can write code), type the following program and save it within the file name assignment1.s (i.e., with a .s extension).
Anything written right side of '#' will be a comment just like "//" in Java or C++.
It is case-sensitive (upper case and lower case make differences.)
Put your name and other information at the top of the file:
###########################################################
# Assignment #: 1
# Name: Your name
# ASU email: Your ASU email
# Course: CSE/EEE230, your lecture time such as MWF 1:30pm # Description: This is my first assembly language program.
# It prints "Hello world".
###########################################################
#data declarations: declare variable names used in program, storage allocated in RAM
.data
message1: .asciiz "Hello world.\n" #create a string containing "Hello world.\n"
#program code is contained below under .text
.text
.globl main #define a global function main
# the program begins execution at main() main:
la $a0, message1 # $a0 = address of message1
li $v0, 4 # $v0 = 4 --- this is to call print_string() syscall # call print_string() jr $ra # return
Note:
li -- "load immediate" la -- "load address":
la $t0, var1 # copy RAM address of var1 into register $t0 jr -- "jump register": jr $ra # jump to the address contained in $ra
3. Once you have typed in the program and saved it, switch back to theQTSpim window and load the program by clicking File -> Load File on the menu bar, and navigating to the location where you saved the file. If your program contains syntax errors, the QTSpim will display the line with the error. If that is the case, you need to go back to the editor window and correct the error and re-save the program. Continue doing this until QTSpim load the program without errors
To execute the program, select Simulator -> Run/Continue (or press the F5 key). You should see "Hello world." displayed in the console window if it is running successfully. You just wrote and executed your first assembly language program!!
4. You can also execute the program by selecting Simulator -> Single Step.This executes one line in the loaded assembly program at a time, and this can be used to debug if it is not producing a correct result.
The program that you have just written has a line using syscall The syscall instruction stands for system call, i.e., it allows the assembly language program to invoke a call to the QTSpim simulator operating system (in a real computer this would be a call the underlying operating system such as
Windows or Linux). Various system calls can be used to request the
QTSpim simulator to perform various functions. Here are some of them:
System Call System Call System Call
Number Operation Description
1 print_int $v0 = 1; $a0 = int to be printed
4 print_string $v0 = 4; $a0 = address of beginning of ASCIIZ string 5 read_int $v0 = 5; user types int at keyboard; value is stored in $v0
8 read_string $v0 = 8; user types string at keybd; addr of beginning of string is stored in $a0; len in $a1
10 exit $v0 = 10; terminates the program
11 print_char $v0 = 11; $a0 = char to be printed
12 read_char $v0 = 12; user types char at keyboard; value isstored in $v0
We are invoking system call number 4 (print_string). Note that the second instruction of the program, li $v0, 4 puts the value 4 in the $v0 register (i.e., the $r2 register). The second instruction, la, $a0, message1, loads the address of the beginning of our message1 string ("Hello world") into the $a0 register (i.e., the $r4 register). This is what is expected by the print_string system call. (Incidentally, you can think of the system call as a function call with the input parameters being stored in registers; if the function returns values, those values will be store in registers as well.)
5. Modify the program to display the string "Goodbye World.\n" Instead of"Hello world.\n". Save this program as assignment1b.s. Be sure to update the program header block. Re-run the program in QTSpim and verify the correct string is displayed.
NOTE: Once you have uploaded a program successfully before, then when you upload a program again, you will need to choose File -> Reinitialize and Load File (instead of Load File) to ignore any main function definition that you have uploaded before. (You can also use Simulator -> Reinitialize Simulator first before loading).
6. If we want to display multiple strings, we can invoke the print_stringsystem call multiple times, once with the address of the beginning of the first string in $a0, and another time with the address of the beginning of another string in $a0 again.
Modify your program to display the following three texts in the console window on separate lines. Update the program header block, save this program as assignment1c.s, and run it.