CPEG323 Homework Review I Long Chen October, 17 th, 2005
Homework 1 Basic Computer Terms Problem 1: Find the correct word that best matches the given description Problem 1: Find the correct word that best matches the given description Problem 2: Classify the items into the categories Problem 2: Classify the items into the categories You need to read Chapter 1 of the textbook to understand/answer these questions
Homework 1 - cont Problem 3 Problem 3 –On average, it takes half a revolution for the desired data on the disk to spin under the read/write head. –Disk is rotating 7200 revolutions per minute (RPM) –Average time for the data to rotate under the disk head? Answer: 1 min / 7200 * ½ Answer: 1 min / 7200 * ½
Homework 1 - cont Problem 4 Problem 4 –One computer issues 30 requests per second –Each request is on average 64KB –Will a 100Mbit Ethernet link be sufficient? Answer Answer –30 * 64 KB/s = 1,920 KB/s = 15,360 Kbit/s –100Mbit/s > 15,360 Kbit/s, sufficient! –B (Byte) versus b (bit) (Bps versus bps)
Homework 2 Problem 1: Problem 1: –Draw the Control Flow Graph (CFG) of code for (i = 0; i < x, i ++) y = y + i; i = 0 i < x y = y + i; i = i + 1; Yes No Similarly, you can group the assembly instructions into basic blocks, then draw the CFG Basic block: no jump, no exit
Homework 2 - cont Problem 2 Problem 2 –A) Why doesn’t MIPS have a subtract immediate instruction? –Answer: MIPS includes add immediate instruction MIPS includes add immediate instruction Positive and negative immediate Positive and negative immediate Add neg imm = sub pos imm Add neg imm = sub pos imm –B) Hexadecimal number -> binary number –C) Binary number -> hexadecimal number
Homework 2 - cont Problem 3 Problem 3 –Implement the C code in MIPS –Draw the status of the stack –Indicate locations of $sp and $fp –Stack pointer: a value denoting the most recently allocated address in a stack –Frame pointer: a value denoting the location of the saved registers and local variables for a given procedure (point to the first word of the frame of a procedure)
Homework 2 - cont Illustration of the stack allocation (a) before, (b) during, and (c) after the procedure call -Reproduce from P & H “Computer Organizatio and Design” 3 rd Edition
Homework 2 - cont int i; void set_array(int num) { int arrray[10]; for (i=0;i<10;i++){ array[i]=compare(num,i); array[i]=compare(num,i);}} set_array: addi $sp, $sp, -52 # move stack pointer sw $fp, 48($sp) # save frame pointer sw $ra, 44($sp) # save return address sw $a0, 40($sp) # save parameter (num) addi $fp, $sp, 48 # establish frame pointer add $s0, $zero, $zero # i = 0 addi $t0, $zero, 10 # max iterations is 10 loop: sll $t1, $s0, 2 # $t1 = i * 4 add $t2, $sp, $t1 # $t2 = address of array[i] add $a0, $a0, $zero # pass num as parameter add $a1, $s0, $zero # pass i as parameter jal compare # call compare(num, i) sw $v0, 0($t2) # array[i] = compare(num, i); addi $s0, $s0, 1# i = i+ 1 bne $s0, $t0, loop # loop if i<10 lw $a0, 40($sp) # restore parameter (num) lw $ra, 44($sp) # restore return address lw $fp, 48($sp) # restore frame pointer addi $sp, $sp, 52 # restore stack pointer jr $ra # return
Homework 2 - cont int compare( int a, int b) { if (sub(a, b) >= 0) return 1; else return 0; } compare: addi $sp, $sp, -8 # move stack pointer sw $fp, 4($sp) # save frame pointer sw $ra, 0($sp) # save return address addi $fp, $sp, 4 # establish frame pointer jal sub # can jump directly to sub slt $v0, $v0, $zero # if sub(a,b) >= 0, return 1 slti $v0, $v0, 1 lw $ra, 0($sp) # restore return address lw $fp, 4($sp) # restore frame pointer addi $sp, $sp, 8 # restore stack pointer jr $ra # return sub: sub $v0, $a0, $a1 # return a-b jr $ra # return int sub( int a, int b) { return a – b; } This function does not change argument registers, so, we do not need to save these registers, i.e. $a0, $a1 We don’t save anything. Why?
Homework 2 - cont The status of the stack
Homework 2 - cont Problem 4 Problem 4 –Comment the following MIPS code –Descript the functionality ($v0 = ?) –As inputs, $a0 = a; $a1 = b add $t0, $zero, $zero loop: beq $a1, $zero, finish add $t0, $t0, $a0 sub $a1, $a1, 1 j loop finish: addi $t0, $t0, 100 add $v0, $t0, $zero # initialize running sum $t0 = 0 # finished when $a1 is 0 # sum $t0 = $t0 + $a0 # compute this $a1 times # add 100 to a * b # return a * b + 100
Homework 2 - cont Problem 5 Problem 5 –Explain why an assembler might have problems directly implementing the branch instruction in the following code segment here:beq$s0, $s2, there … there:add$s0, $s0, $s0
Homework 2 - cont Jump instruction: j10000 Conditional branch instruction: beq$s0, $s2, there New PC = PC + Branch address (PC-relative addressing) bits 26 bits bits 16 bits bits If that address is too far away, we won’t be able to use 16 bits to describe where it is relative to the PC
Homework 2 - cont One simple solution would be here: bne $s0, $s2, skip j there skip:… there: add $s0, $s0, $s0 This will work as long as our program does not cross the 256MB (why?) address boundary described in the elaboration on page 98 in the textbook.
Homework 3 Please refer to the solution to be posted by tonight
Good Luck on your test! Good Luck on your test!