#HW5. #problem 1. # You need 4 unsigned multiplications. # And atleast 4 additions even you assume there is no carry out from the additions. # In reality, carry out happens. # the carry out brings other additions. # The first program below is showing simple addition assuming no carry ont. # # The check routine exams carry out situation since MIPS additon ignore carry out. # And set $v0 to 1 when carry out happends. # # t1 t2 # X t3 t4 #-------------- # t2*t4 # t1*t4 # t2*t3 #t1*t3 #------------- #s1 s2 s3 s4 .data happy_msg: .asciiz "aaa" # .text .globl main main: li $t1, 0x00000011 li $t2, 0x00000011 #0x11111234 li $t3, 0x00000011 #0x11111234 li $t4, 0x11111234 multu $t2,$t4 mfhi $s3 mflo $s4 multu $t1,$t4 mfhi $s2 mflo $t0 add $s3,$t0,$s3 multu $t2,$t3 mflo $t0 add $s3,$t0,$s3 mfhi $t0 add $s2,$t0,$s2 multu $t1,$t3 mflo $t0 add $s2,$t0,$s2 mfhi $t0 add $s1,$t0,$zero CHECK: addi $sp, $sp,-20 sw $t7,0($sp) sw $t8,4($sp) sw $t9,8($sp) sw $t6,12($sp) add $v0,$zero,$zero add $t7,$a0,$0 add $t8,$a1,$0 add $t9,$a2,$0 srl $t7,$t7,31 srl $t8,$t7,31 and $t6,$t7,$t8 sne $v0, $t6,$zero nop bne $v0,$zero,RET #set when both MSB = 1 nop srl $t9,$t9,31 xor $t6,$t7,$t8 # one of the MSB is 0 and the other is 1 bne $t6,$zero, EXAM nop RET: lw $t7,0($sp) lw $t8,4($sp) lw $t9,8($sp) lw $t6,12($sp) addi $sp, $sp,20 j $ra nop EXAM: xor $t6,$t9,$t6 beq $t6,$zero,RET nop addi $v0,$zero,1 j RET nop #Problem 2: # 2-1 # add r1,r2,r4 //divide into each field. # 000000 00010 00100 00001 00000 100000 # //combine for hex. # 0000 0000 0100 0100 0000 1000 0010 0000 # 0x 00440820 # addi r1,r2 0x 20 # 0x 20410020 # nop # 0x 00000000 # beq r0,r0,LOOP # to jump to LOOP you have to go - 3 instructions at non delayed branch # // nop(-1), addi r1,r2,0x20 (-2) add r1,r2,r3 (-3) . # 000100 00000 00000 ( -3 in binary which is 1111 1111 1111 1101) # so...0x1000fffd # to jump to LOOP you have to go - 4 instructions at delayed branch. # //nop(-1), addi r1,r2,0x20 (-2) add r1,r2,r3 (-3) beq r0,r0,LOOP(-4) # 000100 00000 00000(-4 in binary) # so ..0x1000fffc # 2-2 0x400000 j LINE1 # 0x400004 nop # 0x400008 nop # 0x40000c LINE1: beq r0,r0,LINE2 # # j LINE1 becomes j 0x40000c # 000010 0000 0100 0000 0000 0000 0000 11------00(drop 2 bits) # 0000 1000 0001 0000 0000 0000 0000 0011------ # j LINE1 0x 08100003 # nop 0x 00000000 # nop 0x 00000000 # //for non delayed branch # jump 2 instructions so, # beq r0,r0,LINE2 0x10000002 # //for delayed branch # jump 1 instruction so, # 0x10000001 # 2-3 # bne r1,r2,3 # for non delayed branch, jump 3 instructions from 0x400000(3* 4 = 12 =c) # so..0x40000c # bne r1,r2,-3 # for non delayed branch, jump -3 instructions from 0x40000c (-3* 4 = -12 =-c) # so 0x400000 #Problem 3: #Problem 4: # r1 = 3 # r1 = 3 r2 = 5 # r1 = 3 r2 = 5 r3 =-2 # r1 = 3 r2 = 5 r3 =-2 r4 =-2 r31 = 0 # r31 = 0 # r1 = 10101 r2 = 5 r3 = -2 r31 = 0 # r1 = 10101 r2 = 5 r3 = -2 r31 = 0 # r1 = 10101 r2 = 5 r3 = -2 r4 = 0x2000 r31 = 0 # r1 = 10101 r2 = 5 r3 = -2 r4 = 0x2000 r31 = 0x1024 #Problem 5: # F F F F #Problem 6 : # v0,v1: for return value # a0-a1: for argument passing # t0-t7: Temporary,not saved by callee # s0-s7: saved by callee