Download Lab-2 - Suraj @ LUMS

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Organization and Assembly Language Programming
Laboratory Manual
Chapter 2—Jahangir Ikram, April 2008
Task 1 From High-level to Assembly to Machine Code
Exercise 1:
Following is the last exercise from the last lab. It finds sum of 10 numbers stored
in an array myData[10]. Remember index goes from 0 to 9 in C.
.data
myData:
.word 200, -1299, -5000, 7123, 4, -2, 3, -7, 89, 4
sum:
.word 0
# place to hold the final sum
.globl
main
.text
main:
la
$s0, myData
#load address of myData (pointer to array)
la
$s1, sum
#load address of sum, (pointer to sum)
li
$t1, 10
#counter initialized to 10
add
$t2, $zero, $zero #initialize to zero to save the sum, t2 assigned to sum
loop:
lw
add
sw
addi
addi
bne
$t3, 0($s0)
$t2,$t2,$t3
$t2, 0($s1)
$s0, $s0, 4
$t1,$t1, -1
$t1, $t0, loop
#load a number from array
# add array element to sum.
# store the sum, could’ve stored once outside the loop
# increment the base address
#decrement counter
exit:
nop
#end of program
nop
nop
Type and run this program and show its working to the TA.
Exercise 2.
Write equivalent C code for the code above.
Lahore University of Management Sciences
Dr Mohammad Jahangir Ikram
1
Exercise 3. We have seen the C code and Assembly code. Now let us create a processor picture for the above code.
Fill the missing boxes (*) in the following Memory Map and write values in SUM data memory AFTER the sum is stored.
You can watch how sum changes in each loop iteration.
Memory
32-bit HEX
Assembled Code/
Your Assembly
Comments
Address
Instruction or
or data value in dec
Instruction
data
[0x7fffeffc]
0x00000000
Stack area.
●●●
●●●
●●●
[0x1001002c]
[0x10010028] *
*
[0x10010024]
[0x10010020]
[0x1001001c]
[0x10010018]
[0x10010014]
[0x10010010]
[0x1001000c]
[0x10010008]
[0x10010004]
*
*
*
*
*
*
*
*
-1299
*
*
*
*
*
0x00000004
0x00001bd3
0xffffec78
0xfffffaed
Place to store SUM
Confirm the result
Use calculator to confirm that
this 32-bit number is -1299
myData[0] base address
Data Starts here
[0x10010000] 0x000000c8
200
●●●
●●●
●●●
[0x00400054] 0x00000000
[0x00400050] 0x00000000
[0x0040004c] *
nop
nop
*
nop
nop
*
[0x00400048] *
*
*
[0x00400044] *
*
*
[0x00400040] *
*
*
[0x0040003c]
*
*
**
[0x00400038] *
[0x00400034] 0x00005020
[0x00400030] 0x3409000a
*
add $10, $0, $0
ori $9, $0, 10
*
add $t2, $zero, $zero
li $t1, 10
**
[0x0040002c] 0x3431003c
[0x00400028] 0x3c011001
ori $17, $1, 60[sum]
lui $1, 4097 [sum]
la $s1, sum
*
Code ends here
***
**
Here confirm that $s0 and
$s1 has the address of myData
And sum respectively
[0x00400024] 0x3c101001
lui $16, 4097 [myData]
la $s0, myData
Your program code starts here
** On back of the page use 32-bit hex code to prove that this the right instruction in assembly.
*** This is branch instruction. What the branch offset ____________________,
Why_____________________
Lahore University of Management Sciences
Dr Mohammad Jahangir Ikram
2
Exercise 2: SYSTEM CALLS
System call is used to communicate with the system for reading from keyboard or writing to screen.
System call require some parameter to be passed in a particular register and a request number (or service
code) to be passed in an other register $v0.
STEPS FOR USING SYSTEM CALLS
1. Load system call code into Register $v0. Codes (Value of $V0 to be loaded) are given in the table
below.
2. Load arguments (if any) into registers $a0, $a1
3. use syscall
4. Results are returned in registers $v0.
Exercise 0: Printing your name using SYSCALL.
Use the following code to print your name in an output screen.
.data
mesg1: .asciiz “My Name is ………. \n“
.text
.globl main
main:
li
$v0, 4
# Load immediate vi with value 4
la
$a0, mesg1
# a0 points to base address of string array mesg1
syscall
li
$v0, 10
# prepare to exit
syscall
Using table above explain:
1. What does the first SYSCALL do?__________________________
2. What is contained by $a0_________________________________
3. What is purpose of second SYSCALL _________________
4. Modify the above program to print an integer, say 1234.
a. What should be the value of a0 before you make a syscall __________
b. What should be the value of v0 before you make a syscall __________
Lahore University of Management Sciences
Dr Mohammad Jahangir Ikram
3
Exercise 1: Sum of Integers
Type the code given on the last page to find the sum of N integers and run for N = 100, N= 200, N=
1000.
What is the answer in each case?
___________, _______________, _______________, _____________
For what value of N we get a wrong answer and why?
________________
Change the first instruction after label Loop to (add unsigned):
addu
$t0, $t0, $v0
Now for what value of N you start getting wrong answers, can you explain the wrong answer.
Do you get exceptions this time____, why not?__________
What happens if you give a very large N, say 100000. Can you explain the answer now _____________.
EXERCISE 3: Using JAL instruction
Let say we have the following function that returns 0 if the value passed is greater than 255, 0 otherwise.
Convert the following C program to MIPS assembly.
int check255(int x)
{
int result;
if (x > 255) {
result = 0;
else {
result = 1;
}
return result;
}
Here is a template for the program that uses check255 function. The template shows how to call a
function. In addition, it shows how to read from the keyboard and write answer to the screen using SPIM
system calls.
# Using a function in main
.text
.globl main
main:
# get input
addi
$v0, $zero, 5
syscall
add
$a0, $zero, $v0
jal check255
# print output
add
$a0, $zero, $v0
addi
$v0, $zero, 1
syscall
addi
$v0, $zero, 10
syscall
# Use system call 5 (read integer)
# Invoke system call (int is returned in $v0)
# put int into $a0, the argument register for parameter passing
# call check255
# put return value into $a0
# Use system call 1 (print integer)
# Invoke system call
# Use system call 10 (exit)
# Invoke system call
check255:
Lahore University of Management Sciences
Dr Mohammad Jahangir Ikram
4
# put your code here function check255
# make sure the return value is in $v0
jr $ra
# go back to main
What is value stored in $ra when jal check255 is executed ________, Why ________________
EXERCISE 4: Using Stack
Use the following instructions to load values in the register
li $s0, 200
li $s1, -200
li $s2, 400
li $s3, -400
li $t0, 800
li $t1, -800
li $t2, 1000
li $t3, -1000
Now write a code to push all the 8 register values on the stack and fill the table.
Operation
Now write the following code to basically change
All registers
Stack Pointer
$sp
Value
pushed
[0x7fffeffc]
0x0000000
First push
add $s0, $s1, $s0
add $s1, $s2, $s3
add $s2, $s2, $s0
add $s4, $t1, $s1
add $t0, $t1, $s0
add $t1, $t1, $s2
add $t2, $s1, $s2
add $t3, $t1, $t2
add $t4, $s1, $s2
Now pop all values from stack to bring the same
Values as as loaded in the start ans show it to your TA.
What the value off the stack after last pop ____________________
What happens if you try to pop more values?__________________
Lahore University of Management Sciences
Dr Mohammad Jahangir Ikram
5
Comments
Initial Sp
pointing to
0
# Finding Sum of Positive and Negative Numbers
.data
array: .word -5, 4, -10, -22, 20, 12
msg1: .asciiz “\n The sum of the positive numbers is = “
msg2: .asciiz “\n The sum of the negative numbers is = “
.globl main
.text
main:
li
$v0, 4
# system call to print a string
la
$a0, msg1
# a0 has address of message 1
syscall
la
$a0, array
# a0 has base address of array
li
$a1, 6
# load immediate (pseudo instruction), a1 = 6, total data size is 6
jal
sum
# call sum function. On return v0 will have some of pos nos
move $a0, $v0
# a0 has integer to be printed (sum of pos numbers)
li
$v0, 1
# System call for printing a integer
syscall
li
$v0, 4
# system call to print a string
la
$a0, msg2
# a0 has address of message 2
syscall
move $a0, $v1
# a0 has sum of negative numbers
li
$v0, 1
# System call for printing a integer
syscall
li
$v0, 1 0
syscall
# System call program exit
sum:
li
li
$v0,0
$v1,0
Loop:
blez
addi
lw
addi
bltz
add
b
Negative:
add
b
$a1, Return
$a1, $a1, -1
$t0, 0($a0)
$a0, $a0, 4
$t0, Negative
$v0, $v0, $t0
Loop
#a1 has N, if N is less than 0 return from function
# Decrement loop counter
# get a value from array
# increment base pointer
$v1, $v1, $t0
Loop
# sum negative numbers in $v1
Return:
jr
$ra
Lahore University of Management Sciences
Dr Mohammad Jahangir Ikram
6
######################################################
# Program Name: Sum of Integers
# Programmer: YOUR NAME
# Date last modified:
#######################################################
# Functional Description:
# A program to find the sum of the integers from 1 to N, where N is a value
# Read in from the keyboard
# Pseudocode description of algorithm:
# main: count << “\n Please input a value for N = “
#
cin >> v0
#
If ( v0 > 0 )
#
{t0 = 0;
#
while (v0 > 0 ) do
#
{t0 = t0 + v0;
#
v0 = v0 – 1}
#
count << “ The sum of the integers from 1 to N is “, t0;
#
jump to main
#
}
#
else
/*User can input any negative number to terminate this program */
#
cout << “\n **** Bye Bye – Have good day ****”
######################################################################
# Cross References:
# v0: N, t0: Sum
########################################################################
.data
Prompt:
.asciiz
“\n Please Input a value for N = “
Result:
.asciiz
“The sum of the integers from 1 to N is”
Bye:
.asciiz
“\n **** Adios Amigo – Have a good day****”
.glob1
main
.text
main:
li
$v0, 4
# system call code for print string
la
$a0, Prompt
# load address of prompt into $a0
syscall
# print the prompt message
li
$v0, 5
# system call code for Read Integer
syscall
# reads the value of N into $v0
blez
$v0, End
# branch to end if $v0 < = 0
li
$t0, )
# clear register $t0 to 0
Loop:
add
$t0, $t0, $v0
# sum of integers in register $t0
addi
$v0, $v0, -1
# summing integers in reverse order
bnez
$v0, Loop
# branch to loop if $v0 is != 0
End:
li
la
syscall
$v0, 4
$a0, Result
# system call code for Print String
# load address of message into $a0
# print the string
li
move
syscall
b
$v0, 1
$a0, $t0
#
#
#
#
system call code for Print Integer
move value to be printed to $a0
print sum of integers
branch to main
li
la
syscall
li
syscall
$v0, 4
$a0, Bye
#
#
#
#
#
system call code for Print String
load address of msg. into $a0
print the string
terminate program run and
return control to system
Lahore University of Management Sciences
$v0, 10
Dr Mohammad Jahangir Ikram
7