practice (1) fall 2007 mips practice problems: memory transfer, loops, conditionals arrays
Post on 19-Dec-2015
226 views
TRANSCRIPT
Practice (1) Fall 2007
MIPS Practice Problems:Memory Transfer, Loops,
ConditionalsArrays
Practice (2) Fall 2007
Assembly programming life cycle
Prof. Lo assigns HW
You start programming at 3 AM on due date
SPIM/MIPSAssemblycode
SPIM AssemblerMIPS(Object)Code Operating
system
Fetch-execute-fetch-execute-fetch-execute
Practice (3) Fall 2007
Anatomy of a SPIM/MIPS program(covered in detail in discussion Friday)
Practice (4) Fall 2007
.dataA: .word 0B: .word 15C: .word 20D: .word 25E: .word 30Write code to compute
A = (B + C) - (D + E)
Practice (5) Fall 2007
.dataA: .word 0B: .word 15C: .word 20D: .word 25E: .word 30Write code to compute this exp with 3 registers
A = (B + C) - (D + E)
LW $s2,B
LW $s3,C
ADD $s2, $s2, $s3
LW $s3, D
LW $s4, E
ADD $s3, $s3, $s4
SUB $s2, $s2, $s3
SW $s2, A
Practice (6) Fall 2007
.dataA: .word 0, 15, 20, 25, 30 #A=0;B=15;C=20;D=25;E=30Write code to compute this exp
A = (B + C) - (D + E)
Practice (7) Fall 2007
.dataA: .word 0, 15, 20, 25, 30 #A=0;B=15;C=20;D=25;E=30Write code to compute this exp
A = (B + C) - (D + E)
LA $s1, A
LW $s2, 4($s1)
LW $s3, 8($s1)
ADD $s2, $s2, $s3
LW $s3, 12($s1)
LW $s4, 16($s1)
ADD $s3, $s3, $s4
SUB $s2, $s2, $s3
SW $s2, A # Also SW $s2, 0($s1)
Practice (8) Fall 2007
.dataA: .word 0,0,0,0,0 #array A has 5 elmtsWrite code to compute
A[0] = A[3] + A[2]
Practice (9) Fall 2007
.dataA: .word 0,0,0,0,0 #array A has 5 elmtsWrite code to compute
A[0] = A[3] + A[2]
LA $s1, A
LW $s2, 12($s1)
LW $s3, 8($s1)
ADD $s2, $s2, $s3
SW $s2, 0($s1) # or SW $s2, A
Practice (10) Fall 2007
.dataDUCKS: .word 24BEARS: .word 31Write code to swap the values of DUCKS and BEARS
Practice (11) Fall 2007
.dataDUCKS: .word 24BEARS: .word 31Write code to swap the values of DUCKS and BEARS
LW $s1, DUCKS
LW $s2, BEARS
SW $s1, BEARS
SW $s2, DUCKS
BEQ $S1, $s2, SHOULDABEENATIE
Practice (12) Fall 2007
.dataA: .word 10B: .word 15C: .word 20Write code to change all values to 77
Practice (13) Fall 2007
.dataA: .word 10B: .word 15C: .word 20Write code to change all values to 77
LI $s1,77
SW $s1,A
SW $s1,B
SW $s1,C
Practice (14) Fall 2007
.dataA: .word 0,0,0,0,0,0,0,0,0,0Write code to change all values to 77(A[0] through A[9])
Practice (15) Fall 2007
.dataA: .word 0,0,0,0,0,0,0,0,0,0Write code to change all values to 77(A[0] through A[9])
LI $s7,77 # value 77LI $s1,10 #loop counterLI $s2, 0 # initial array indexLA $s3, A # base addr
AGAIN:SLL $s2,$s2,2 # multiply by 4ADD $s4,$s3,$s2 # new array elmtSW $s7, 0($s4) ADDI $s1,-1BEQ $s1,$0,DONEADDI $s2,1 # next array indexJ AGAIN
DONE:
Practice (16) Fall 2007
.dataA: .word <some value>B: .word <some value>C: .word 0
Write code to store the bigger value of A or B into C.
Practice (17) Fall 2007
.dataA: .word <some value>B: .word <some value>C: .word 0
Write code to store the bigger value of A or B into C.
LW $s1,A
LW $s2,B
BGE $s1,$s2,BIGA
SW $s2,C
J DONE #Must jump around BIGA
BIGA: SW $s1,C
Practice (18) Fall 2007
.dataMCL: .word 1 # 1 = trueACL: .word 0 # 0 = false
Write code to figure out if Ginnie has torn her Medial Collateral Ligament or her Anterior Cruciate Ligament.
Practice (19) Fall 2007
.dataMCL: .word <0 or 1> # 1 = trueACL: .word <0 or 1> # 0 = false
Write code to figure out if Ginnie has torn her Medial Collateral Ligament or her Anterior Cruciate Ligament.
lw $s1, MCLlw $s2, ACLblt $s1, $2, ACLONLY #MCL 0; ACL 1blt $s2, $s1,MCLONLY #ACL 0; MCL 1beq $s1, $0, HEALTHY<print both ACL and MCL>J HOSPITAL # need all these jumps
ACLONLY: <print ACL>J HOSPITAL
MCLONLY: <print MCL>J HOSPITAL
Practice (20) Fall 2007
.dataBIG: .word <one hundred values>ENDBIG: .word
Write code to reverse the elements in array BIG.
Practice (21) Fall 2007
.dataBIG: .word <one hundred values>ENDBIG: .word
Write code to reverse the elements in array BIG.
LA $s2,BIGLA $S3,ENDBIGADDI $s4,$s2,200 #halfway
LUP: LW $t2,0($s2)LW $t3,0($s3)SW $t2,0($s3)SW $t3,0($s2)ADDI $s2,$s2,4SUBI $s3,$s3,4BNE $s2,$s4,LUP
Practice (22) Fall 2007
.dataBIG: .word <one hundred values>ENDBIG: .word
Write code to print the elements from array BIG.
LA $s1,BIGLA $s2,ENDBIG
LOOP:LW $s3,0($s1)MOV $a0,$s3 #load value to printLI $v0,1 #code for print integersyscall #go to OS to printADDI $s1,$s1,4 #next valueBNE $s1,$s2,LOOP
Practice (23) Fall 2007
.dataBIG: .word <one hundred values>ENDBIG: .word
Write code to print the elements frpm array BIG in reverse order.
<no answer provided>
Practice (24) Fall 2007
.dataBIG: .word <one hundred values>SMALL: .word <one hundred values>
Write code to compute the following assuming $s1 contains the value END > 40:
For (I = 40; I <= END; I++)
BIG[I]=SMALL[I+3];
<no answer provided>