final cheatsheet230.docx -...

This preview shows page 1 - 2 out of 2 pages.

FiveCmpOfCpu-control,datapath,memory,input,outputWhen given j label and jump address…Discard first 4 bits, divide by 4 = constWhen given j label, const, and PC(PC+4), then const*4, then append last 4 of (PC+4)When given j label, PC & j addressconst = Delete last 4 bits, then divide by 4When given bne/beq w/PC & BA(BA-PC-4)/4 = constWhen given bne/beq w/ PC…BranchAddr = PC + 4 + const * 4 -> convert if necessaryWhen given j label, PC ,and const…Append last 4 of (PC+4) toArray1[4]=array[3]-h+array[10]#base address of array1 is $s0, h is in $s1lw $t0, 12($s0) #$t0 = array1[3]sub $t2, $t0, $s1 #$t2 = array1[3]-hlw $t1, 0($s0) #$t1 = array1[0]add $t3, $t2, $t1 #$t3 = (array1[3] - h) + array1[0]sw $t3, 16($s0) #array1[4] = $t3RecursionAnytime there is jal, store $ra using $sp, then when you leave the function restore the stackStore $a0 before jal if you need old valueInt funct(int n){If( n==1) { return 1; }else if(n ==2){ return 2; }else{ return funct(n-1)+funct(n-2)-n;}jal jal$a0-$$a3 for params(store $ra using $sp, store $a0)$v0 for return values#parameter n -> $a0Jal call function#return value -> $v0jr $ra to returnfunct:#save $ra return address, $a0 parameter & $s3 parameteraddi $sp, $sp, -4sw $ra, 0($sp)addi $sp, $sp, -4sw $a0, 0($sp)addi $sp, $sp, -4sw $s3, 0($sp)#end save stackli $s1, 1 #$s1 = 1bne $a0, $s1, ElseIf#load $s3 var, parameter value $a0 and return addr $ralw $s3, 0($sp)addi $sp, 4lw $a0, 0($sp)addi $sp, $sp, 4lw $ra, 0($sp)addi $sp, $sp, 4jr $ra #returnElseIf:Li $s2, 2 #$s2 = 2Bne $a0, $s2, ElseLi $v0, 2###same as bne (restore the stack)Else: #recursive case#call funct(n-1)addi $a0, $a0, -1 #$a0 = n-1jal funct #$v0 = funct(n-1)move $s3, $v0 #$s3 = funct(n-1)addi $a0, $a0, -1 #$a0 = (n-1) -1 = n-2jal funct#$v0 = funct(n-2)Addi $a0, $a0, 2 #$a0 = (n-2) +2 = n$v0 = $s3 + $v0 - $a0 # = answer###restore the stack and jr $ra$a0-$a3 for parameters$v0 - $v1 for return valuesJal -> to call functionjr $ra -> to returnfor(int I = 3; I < end; i++){ //s0=end; s1=addr of nums; s2=I;nums[i] = nums[i]*16; }li $s2, 3 # $s2 = i, i = 3Loop:slt $t1, $s2, $s0 # $t1 = 1 if i < end, 0 otherwisebeq $t1, $zero, Exit # if i >= end,# i.e., if $t1=0, then branch to Exitsll $t2, $s2, 2 # temp register $t2 = i*4add $t3, $s1, $t2 # $t3 = address of numbers[i]# = (address of numbers) +(i*4)lw $t4, 0($t3) # $t4 = numbers[i]sll $t4, $t4, 4 # $t4 = numbers[i]*16sw $t4, 0($t3) # numbers[i] = $t4 (= numbers[i]*16)addi $s2, $s2, 1 # i = i + 1j Loop # go back to Loop to repeatExit:Machine code Assembly1) Convert to binary2) Opcode to determine type (0=R-type, <0 green sheet3) Split up the rest of the bits according to type4) Convert the decimal to identify registers5) Convert function code bits to hex to find operationGiven machine code:Beq ….. Label \nBne ……LabelopcodersrtImm/ cons… (BranchAddr)PC = 00400020hex What Is the address that to branches to when the condition holds?BranchAddr = const*4 + (PC+4) (sll 2 bits = *4Given machine code for j Label200001000………………………0100 (26 bits)PC = 20400020hexWhat is the address it jumps to?1) add 4 to PC -> PC+4 = 20400024hex2) const *4, 3) add last 4 bits of pc+4 to imm/const * 4 (sll 2)00…………………….0100 * 4001000…………010000For j label2PC = 20400020hexjumpAddr = 20000010hexWhat is its machine code?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture