CS代写 COMP2421 Computer Organization 2019/20 Semester 1

COMP2421 Computer Organization 2019/20 Semester 1
Lab 3 MIPS Assembly Language Basics
In this lab, basic syntaxes of MIPS assembly language will be introduced, which include: ➢ Performing system calls
➢ Loading address instruction

Copyright By PowCoder代写 加微信 powcoder

➢ Loading integer instruction
➢ Performing arithmetic instructions
A Closer Look of Your First Program Recall “lab01.s” in Lab 1:
################################################################# # File: lab01.s # ## # This program is to perform two calculations, an addition and # # a subtraction. It first asks to input 2 integers through the # # console and then does the calculations. Finally, it prints # # out the results on the console. # #################################################################
####################
# The data segment #
####################
# Create some null terminated strings which are to be used in the program
strPromptFirst: .asciiz “Enter the first operand (A): ”
strPromptSecond: .asciiz “Enter the second operand (B): ”
strResultAdd:
strResultSub:
.asciiz”A+Bis” .asciiz”A-Bis” .asciiz “DONE\n” .asciiz “\n”
###############################################
# The text segment — instructions start here #
###############################################
.globl main
# STEP 1 — get the first operand
# Print a prompt asking user for input
li $v0, 4 # syscall number 4 prints string whose address is in

COMP2421 Computer Organization
2019/20 Semester 1
la $a0, strPromptFirst # “load address” of the string
syscall # actually print the string
# Now read in the first operand
la $a0, strPromptSecond
# syscall number 4 prints string whose address is in
# “load address” of the string # actually print the string
move $s0, $v0
# syscall number 5 reads an int
# actually read the int
# save result in $s0 for later
# STEP 2 — repeat above steps to get the second operand
# First print the prompt
# Now read in the second operand
move $s1, $v0
# syscall number 5 reads an int
# actually read the int
# save result in $s1 for later
# STEP 3 — print the sum
# First print the string prelude
la $a0, strResultAdd
# Then print the actual sum
add $a0, $s0, $s1
# syscall number 4 prints string
# actually print the string
# syscall number 1 prints int
# add operands and put result in $a0 for printing
# actually print the int
# Finally print a carriage return
la $a0, strCR
# syscall for printing string
# address of string with a carriage return
# actually print the string
# STEP 4 — print the difference
# First print the string prelude
la $a0, strResultSub
# Then print the actual sum
sub $a0, $s0, $s1
# syscall number 4 prints string
# actually print the string
# syscall number 1 prints int
# add operands and put result in $a0 for printing
# actually print the string
# Finally print a carriage return
li $v0, 4 # syscall for printing string
la $a0, strCR

COMP2421 Computer Organization 2019/20 Semester 1
# STEP 5 — EXIT
li $v0, 10
##################
# End of Program #
##################
# Syscall number 10 is to terminate the program
# exit now
In this example, we have strings (char arrays) with different labels, e.g., “strPromptFirst”. To print out a string, syscall (system call or kernel call) is used to instruct the simulator. Moreover, the example shows how to get inputs from user and display the calculation result through the “console”.
Program Structure
MIPS has a variable declaration section named “.data” (assembler directive). In addition, program codes (instructions) are placed in “.text”. You can place your comments in the program anywhere by using the symbol “#” in a line. Below is a MIPS program template:
# File Name: Template.s
# Description: Bare-bones outline of MIPS assembly language program # Author: Put your name here
.data # variable declarations follow this line # …
.text # instructions follow this line
main: # indicates start of code (first instruction to execute) # …
# End of program, leave a blank line afterwards to make SPIM happy.

COMP2421 Computer Organization 2019/20 Semester 1
A directive is a message that tells the assembler something it needs to know in order to carry out the assembly process. This includes indicating where the variables are declared or the instructions are defined. (Note: Assembler directives are not executable statements.)
Here are some common assembler directives:
.asciiz str
Description
Store the string in memory and null-terminate it.
.data
The following data items should be stored in the data segment of the memory. If the optional argument addr is present, the items are stored beginning at address addr.
.text
The next items are put in the text segment. In SPIM, these items may only be instructions or words (see the .word directive below). If the optional argument addr is present, the items are stored beginning at address addr.
.globl sym
Declare that symbol sym is global and can be referenced from other files.
For example, .globl main means that the identifier main will be used outside of this source file (i.e., used globally) as the label of a particular location in main memory.
.word w1, …, wn
.byte b1, …, bn
Allocate n bytes of space in the current segment (which must be the data segment in SPIM).
Store the n 32-bit quantities in successive memory words. Store the n values in successive bytes of memory.
.extern sym size
Declare that the datum stored at sym is size-byte large and is a global symbol. This directive enables the assembler to store the datum in a portion of the data segment that is efficiently accessed via register $gp.

COMP2421 Computer Organization
2019/20 Semester 1
Data Types
CPU Registers
8-bit integer
16-bit integer
32-bit integer
32-bit floating-point number 64-bit floating-point number
A CPU register is a memory location, normally small (e.g., 8/16/32/64 bits) in size, accessible directly by the CPU. A register may hold an instruction, a storage address, or any kind of data (such as a bit sequence or individual characters).
Register Name
$a0-$a3 $t0 – $t7
Register Number
$4-$7 $8 – $15
Register Usage
Hardware set to 0
Assembler temporary (reserved by the assembler)
Function result (low/high)
Argument Registers – First four parameters for subroutine. Not preserved across procedure calls. Temporary registers – Caller saved if needed. Subroutines can use without saving. Not preserved across procedure calls.
Temporary registers. (These are in addition to $t0 – $t7 above.)
Reserved for OS kernel (use by interrupt/trap handler)
Global pointer – points to the middle of the 64K block of memory in the static data segment.
Stack pointer – points to last location on the stack. Frame pointer – saved value. Preserved across procedure calls
Return address
Saved registers – Callee saved. A sub-routine using one of these must save original and restore it before exiting.
Preserved across procedure calls.

COMP2421 Computer Organization 2019/20 Semester 1
System Calls
System calls are APIs for the interface between the user space and the kernel space. In other words, it allows the use of functions provided by the OS, like printing a value on the screen. To issue a system call, a numeric code has to be put in the register in $v0 first.
print_int print_float print_double print_strint read_int read_float read_double
Code in $v0 Arguments
integer returned in $v0 float returned in $f0 double returned in $f0
1 2 3 4 5 6 7
$a0 = integer to be printed
$f12 = float to be printed
$f12 = double to be printed
$a0 = address of string in memory
read_string
$a0 = memory address of string input buffer
$a1 = length of string buffer (n)
$a0 = amount
To allocate memory dynamically starting from the address in $v0. It is used in Unix/Linux machines.
For example, the following statement “move” a data value from one register to another one and then use a system call to print out the value.
move $a0, $t0 li $v0, 1 syscall
# move value from $t0 to $a0
# use a system call to print out integer # print the value on console

COMP2421 Computer Organization 2019/20 Semester 1
Load / Store Instructions
Instruction Description
Load word or copy word (4 bytes) at lw register_destination, RAM_sourcesource RAM location to destination
lb register_destination, RAM_source
Load byte or copy byte at source RAM location to low-order byte of destination register.
li register_destination, value
sw register_source,RAM_destinationStore word (4 bytes) in source
Load immediate value into destination register.
register location to RAM destination.
sb register_source, RAM_destinationStore byte (low-order) in source register location to RAM destination.
Arithmetic Instructions Instruction Syntax
add $t0, $t1, $t2 sub $t0, $t1, $t2 addi $t0, $t1, 5 addu $t0, $t1, $t2 subu $t0, $t1, $t2
div $t1,$t2
mflo $t0 move $t1, $t2
Description
# $t0 = $t1 + $t2;
add as signed integers
# $t0 = $t1 – $t2;
subtract as signed integers
# $t0 = $t1 + 5;
add immediate
# $t0 = $t1 + $t2;
add as unsigned integers
# $t0 = $t1 – $t2;
subtract as unsigned integers
# Lo = $t1 / $t2; (integer quotient)
# Hi = $t1 mod t2;(remainder)
# $t0 = Hi
move quantity in special register Hi to $t0
# $t0 = Lo
move quantity in special register Lo to $t0 # $t1 = $t2
mult $t1, $t2
# (Hi, Lo) = $t1 * $t2;
multiply 32-bit quantities in $t1 and $t2, and store 64- bit result in special registers Lo and Hi

COMP2421 Computer Organization
2019/20 Semester 1
Example – BMI Calculator
# This program returns Body Mass Index (BMI) value
# Procedures:
#1. Print message: “Enter Weight (whole pound): ”
#2. Read the input integer from the console
#3. Print message: “Enter Height (whole inch): ”
#4. Read the input integer from the console
#5. Calculate the BMI
#6. Show the result on Console: “Your BMI is: ”
str1: .asciiz “Enter Weight (whole pound): ”
str2: .asciiz “Enter Height (whole inch): ”
str3: .asciiz “Your BMI is: ”
.globl main # Global variable: the entry point of the prog.
.text main:
#Step 1: Print the prompt message using system call 4 #
la $a0, str1 # load string address into $a0 and I/O code into $v0 li $v0, 4
syscall # print the message on console
#Step 2: Read the integer from the console using system call 5
move $s0, $v0
#Step 3: Repeat Step 1 and Step to read in “Height”
la $a0, str2 # load string address into $a0 and I/O code into $v0 li $v0, 4
move $s1, $v0
#Step 4: Calculate the BMI (mass * 703) / (height)^2 #
li $t0, 703
mult $t0, $s0
mult $s1, $s1
div $t1, $t2
# print the message on console

COMP2421 Computer Organization 2019/20 Semester 1
#Step 5: Print the result message using system call 4
la $a0, str3 # load string address into $a0 and I/O code into $v0 li $v0, 4
syscall # print the message on console
#Step 6: Print the BMI
move $a0, $s2
li $v0, 10
# syscall code 10 for terminating the program
Debugging and Tracing
QtSpim provides useful tools to facilitate program debugging.
Single Step
Run “lab01.s” by pressing the function key [F10]. The program will be executed statement by statement. The “Main” window shows the program and shows that which registers are being used during the program execution. In “Text” section, the program statements are shown. Press the function key [F10] until a message is printed on the “Console” window.

COMP2421 Computer Organization 2019/20 Semester 1
Note: the line being highlighted is the next to be executed.
[Question: What is the value of the register $v0? How about $a0? What is the meaning of these values?]
When we debug a program, we would like to know if the program is running as expected. A common technique is to check the variable values (and their changes). For assembly language, we check the values of the registers. Besides, the logic of the program has to be traced.
Breakpoints
Other than executing the program line by line, we can set “breakpoints”. Breakpoints allow the program to pause at particular statement(s). To use breakpoint in QtSpim, right click on the address of the selected statement and choose [Set Breakpoint].
For example, we want to check that whether the two registers hold the input values correctly. We set a breakpoint at the statement after accepting the two inputs. Once the execution reaches the breakpoint, it pauses.

COMP2421 Computer Organization 2019/20 Semester 1
Click [Single Step]. Examine the registers.
[Exercise]
Write a program which converts temperature in Celsius (C) to Fahrenheit (F).
𝑭=𝑪 × 𝟗+𝟑𝟐 𝟓

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com