Solutions - USC Bits

EE 109 Homework 5
Name: ___________________________________________
Due:
Note: Attach all work to receive full credit
Score: ________
HLL to Assembly Translation
1.) Translate the following C code statements to an equivalent assembly language
implementation.
short data[20];
...
for(int i=0;i < 20; i++){
data[i] = data[i] + 5;
}
// let i be stored in $t0
data .space
.text
...
la
add
li
L0: slt
L1: beq_
L2: sll
L3: add
L4: lh
L5: addi
L6: sh
L7: addi
L8: b
L9: ...
x
y
int *x, *y;
...
if(*x >= 10 && *y < 5)
code A
else if (*x < 10 || *y > 5)
code B
else
code C
...
L1:
L2:
L3:
L4:
L5:
L6:
L7:
L8:
.space
.space
...
la
la
addi
addi
lw
lw
lw
lw
blt
bge_
Code A
b
blt_
ble_
Code B
b
Code C
...
40 or 0x28___
$s0,data
$t0,$zero_,$zero
$t4,_20 or 0x14__
$t1,$t0_,$t4
$t1,$zero,_L9___
$t2,$t0,_1__
$s1,$s0_,$t2
$t3,0($s1)__
$t3,$t3_,5
$t3,0($s1)__
$t0,$t0,1
_L0_____
4
4
$s0,x
$s1,y
$t0,$zero_,10
$t1,$zero_,5
$s2,0($s0)
$s3,0($s1)
$t2,0($s2)_
$t3,0($s3)_
$t2_,$t0,_L3___
$t3,$t1_,_L3___
instructions
L8___________
$t2,$t0,_L5___
$t3,$t1,_L7___
instructions
_L8__________
instructions
Subroutines and Assembly
2.) In addition to passing arguments in registers or by using the stack, another possible
method (that no one would ever use) is to provide space for the arguments in the code
itself (though you should note that this code will NOT assemble using MARS because it
does not allow data directives in the text section). However, pleas study the code below
to understand how this method works and answer the given questions. Show any work
on an extra page and submit that with your homework. SHOW ALL VALUES in HEX.
Assume that the .data section starts at 0x10010000.
a.) Before execution of “jal AVG”, what are the contents of $t0, $t1, and $t2.
b.) Before execution of “sra $v0,$v0,1”, what are the contents of $v0, $t3, & $ra?
c.) After program execution, what are the contents of $ra and the word at address
RES.
0x400000
0x400008
0x40000c
0x400010
0x400014
0x400018
0x40001c
0x400020
0x400024
0x40002c
0x400030
0x400038
.data
VALS: .half 0xbead, 0xface
RES: .space 4
.text
la
$t0,VALS
lh
$t1,($t0)
lhu
$t2,2($t0)
sll
$t2,$t2,16
or
$t2,$t1,$t2
jal
AVG
.word
-2
.word
-6
la
$t4,RES
sw
$v0,($t4)
li
$v0,10
syscall
AVG: lw
lw
add
sra
addi
jr
$v0,($ra)
$t3,4($ra)
$v0,$v0,$t3
$v0,$v0,1
$ra,$ra,8
$ra
a.)$t0 = 0x10010000
$t1 = 0xffffbead
$t2 = 0xffffbead
b.)$v0 = 0xfffffff8
$t3 = 0xfffffffa
$ra = 0x0040001c
c.)$ra = 0x00400024
M[RES] = 0xfffffffc