程序代写代做代考 assembly ECE331: Hardware Organization and Design Summer 2018-Session1

ECE331: Hardware Organization and Design Summer 2018-Session1
Lab # 1 solutions
1. Questions
a) The int registers before and after the program is executed.
The register values depend on the name and length in the program. For example, the v0 register should be the “sum” of the name.
b) What is the structure of the an activation record for a function? Give example of what is stored in the stack for an activation record?
An activation record is the space allocated on stack to keep the running information of a procedure, including the return address, callee saved registers (used to hold local variables), input arguments and etc. The procedure stores these values into the stack in case the called procedure compromises the values stored in general registers.
c) How many times is GetNumber called for your program?
This number depends on the length of the name in the program.
d) What is the size of the biggest program stack during the entire running period? When does it happen?
In the main function, the biggest stack happens, and the size of the stack can be upto 16 + 8 bytes. (In the reference code, I choose a simple way and don’t put the variable “temp” in the stack. As a result, only 16 bytes are used).
e) How many assembly instructions are there in your program? This number depends on the code you write.

2. Reference code
.globl main
.data Name:
.ascii “russelltessier”
.text
lui $sp 0x8000 #initialize the stack pointer #######sub function ————–main rountine
main:
addiu
sw
la
sw
sw
sw
addi
addi
addi
$sp, $sp, -16 $ra, 12($sp)
#stack grows by 16 bytes
#save return address
#get the base address of the array
$t0, Name $s0, 8($sp) $s1, 4($sp) $s2, 0($sp)
#$s0 is used to hold the variable length #$s1 is used to hold the variable sum #$s2 is used to hold the variable i
$s0, $zero,1 $s1,$zero,0 $s2,$zero,0
#length = 14 #sum = 0
#i = 0
loop: slt beq add lbu add jal nop add addi j
exit:
add
used for the syscall
lw $ra, 12($sp) lw $s0, 8($sp) lw $s1, 4($sp) lw $s2, 0($sp)
$t1, $s2, $s0 $t1, $zero, exit $t2, $t0, $s2
$t3, 0($t2) $a0, $t3, $zero
GetNumber
$s1, $v0, $s1 $s2, $s2, 1 loop
#if i < length, t1 =1, else t1 = 0 #if t1 = 0, end loop #address of Name[i] #load the character at Name[i] #pass the arguments #call GetNumber #sum = sum + temp #i++ #return sum to v1, because v0 is #restore return address #restore s0 $v1, $s1, $zero addiu #jr li syscall .end main #release the stack space #stack grows by 8 bytes #store return address in stack #s0 is used to hold the variable #initialize s0 to 0 #temp = letter - ‘a' + 1 #return temp #release the stack addiu sw sw temp addiu sub addiu add lw lw addiu jr .end GetNumber $sp, $sp, 16 $ra $v0, 10 #######sub function --------------GetNumber GetNumber: $sp, $sp, -8 $ra, 4($sp) $s0, 0($sp) $s0, $zero, 0 $s0, $a0, 'a' $s0, $s0, 1 $v0, $s0, $zero $s0, 0($sp) $ra, 4($sp) $sp, $sp, 8 $ra