Download presentation
Presentation is loading. Please wait.
Published byAlexandrina Lester Modified over 8 years ago
1
CPEG323 Homework Review I Long Chen October, 17 th, 2005
2
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
3
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 * ½
4
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)
5
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
6
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
7
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)
8
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
9
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
10
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?
11
Homework 2 - cont The status of the stack
12
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
13
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
14
Homework 2 - cont Jump instruction: j10000 Conditional branch instruction: beq$s0, $s2, there New PC = PC + Branch address (PC-relative addressing) 100002 6 bits 26 bits 165 6 bits 16 bits 1816 5 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
15
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.
16
Homework 3 Please refer to the solution to be posted by tonight
17
Good Luck on your test! Good Luck on your test!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.