Download presentation

Presentation is loading. Please wait.

Published byWinston Wiswell Modified about 1 year ago

1
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly Instructions, cont’d Maciej Ciesielski

2
ECE 232 L6.Assemb.2 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Administrative issues °Homework #2 assigned today, due Th., Feb. 21 in class. °Excuses that cannot be tolerated Forgot to buy the textbook Forgot to do the homework Dog ate the homework, etc … (you are pretty creative at those) °Solving the textbook problem Putting one copy of text on reserve in Physical Science Library °Change in late homework policy 25% per day – to allow to post solutions in 3-4 days °Midterm exam I – Th., March 7, 6:30 pm Morrill Science Center (Bldg II) room 131 No excuses (forgot, overslept, overate, etc) Only legitimate reasons (in writing) can be accepted

3
ECE 232 L6.Assemb.3 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Outline °Review: MIPS instructions, formats MIPS instructions: data transfers, arithmetic, logical Pseudo-instruction example: loading large constant MIPS register organization °Implementing loops °Implementing switch/case statement °Procedures and subroutines Stacks and pointers °Running a program Compiler, Assembler, Linker, Loader

4
ECE 232 L6.Assemb.4 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers MIPS Addressing Modes/Instruction Formats immedoprsrt Immediate All instructions are 32-bit wide Base+index immedoprsrt register + Memory PC-relative immedoprsrt PC Memory + register Register (direct ) oprsrtrdfnc...

5
ECE 232 L6.Assemb.5 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers R0$zero constant 0 R1$at reserved for assembler R2$v0 value registers & R3$v1 function results R4$a0 arguments R5$a1 R6$a2 R7$a3 R8$t0 temporary: caller saves... (callee can clobber) R15 $t7 MIPS: Software conventions for registers R16 $s0callee saves... (caller can clobber) R23 $s7 R24 $t8 temporary (cont’d) R25 $t9 R26 $k0 reserved for OS kernel R27 $k1 R28 $gp pointer to global area R29 $spStack pointer R30 $fpFrame pointer R31 $ra return Address

6
ECE 232 L6.Assemb.6 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers MIPS data transfer instructions Instruction Comment sw 500($r4), $r3Store word sh 502($r2), $r3Store half sb 41($r3), $r2Store byte lw $r1, 30($r2)Load word lh $r1, 40($r3)Load halfword lhu $r1, 40($r3)Load halfword unsigned lb $r1, 40($r3)Load byte lbu $r1, 40($r3)Load byte unsigned lui $r1, 40Load Upper Immediate (16 bits shifted left by 16) 0000 … 0000 LUI $r5 $r5

7
ECE 232 L6.Assemb.7 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Loading large numbers °Pseudo-instruction li $t0, big: load 32-bit constant lui $t0, upper# $t0 [31:16] upper ori $t0, $t0, lower# $t0 ($t0 Or [0ext.lower]) $to: upper lower upper lower Or 32-bit constant

8
ECE 232 L6.Assemb.8 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Loop with variable array index °Compile the following loop, with A[ ] = array with base address in $s5; variables g, h, i, j associated with registers $s1, $s2, $s3, $s4. Loop:g = g + A[i]; i = i + j; if (i h) go to Loop; MIPS instructions: Loop:add $t1, $s3, $s3 # $t1 i+i = 2i add $t1, $t1, $t1 # $t1 2i+2i = 4i add $t1, $t1, $s5 # $t1 address of A[i] lw $t0, 0 ($t1) # $t0 A[i] add $s1, $s1, $t0 # $s1 g + A[i] add $s3, $s3, $s4 # $s3 i + j bne $s3, $s2, Loop # if (i h) go to Loop

9
ECE 232 L6.Assemb.9 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers While loop °Base address of A[i] is in $s6; variables i, j, k are in $s3, $s4, $s5. °Compile the following while loop while (A[i] == k) i = i + j; into MIPS instructions: Loop:add $t1, $s3, $s3 # $t1 i+i = 2i add $t1, $t1, $t1 # $t1 2i+2i = 4i add $t1, $t1, $s6 # $t1 address of A[i] lw $t0, 0 ($t1) # $t0 A[i] bne $t0, $s5, Exit # if (A[I] k) go to Exit add $s3, $s3, $s4 # $s3 i + j j Loop # go to Loop Exit:

10
ECE 232 L6.Assemb.10 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Switch/case statement °Variables f - k are in $s0 - $s5. Register $t2 contains constant 4. °Compile the following switch statement into MIPS instructions switch (k) { case 0: f = i + j; break; /* k=0 */ case 1: f = g + h; break; /* k=1 */ case 2: f = g - h; break; /* k=2 */ case 3: f = i - j; break; /* k=3 */ } °Use the switch variable k to index the jump address table. First, test for k, if in correct range (0-3). slt $t3, $s5, $zero # test if k, 0 bne $t3, $zero, Exit # if k < 0, go to Exit slt $t3, $s5, $t2 # test if k < 4 beq $t3, $zero, Exit # if k 4, go to Exit

11
ECE 232 L6.Assemb.11 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Switch statement, cont’d °Access jump table T [ ] with addresses L0,L1,L2,L3: add $t1, $s5, $s5 # $t1 2k add $t1, $t1, $t1 # $t1 4k add $t1, $t1, $t4 # $t1 address of T [k] lw $t0, 0 ($t1) # $t0 T [k] °Use jump register instruction to jump via $t1 to the right address jr $to # jump based on register $t0 L0:add $s0, $s3, $s4 # k = 0, so f=$s0 i + j j Exit # go to Exit L1:add $s0, $s1, $s2 # k = 1, so f=$s0 g + h j Exit # go to Exit L2:sub $s0, $s1, $s2 # k = 2, so f=$s0 g + h j Exit # go to Exit L3:sub $s0, $s3, $s4 # k = 3, so f=$s0 i - j Exit:

12
ECE 232 L6.Assemb.12 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Stacks in Procedure Calls Stacking of Subroutine Calls and Returns: A: CALL B CALL C C: RET B: A AB ABC AB A Some machines provide a memory stack as part of the architecture (e.g., VAX) Sometimes stacks are implemented via software convention (e.g., MIPS)

13
ECE 232 L6.Assemb.13 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Memory Stacks Useful for stacked environments/subroutine call and return Stacks that Grow Up vs. Stacks that Grow Down: a b c 0 Little inf. Big 0 Little inf. Big Memory Addresses SP Next Empty? Last Full? How is empty stack represented? Little --> Big/Last Full POP: Read from Mem(SP) Decrement SP PUSH: Increment SP Write to Mem(SP) grows up grows down Little --> Big/Next Empty POP: Decrement SP Read from Mem(SP) PUSH: Write to Mem(SP) Increment SP

14
ECE 232 L6.Assemb.14 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Call-Return Linkage: Stack Frames Reference args and local variables at fixed (positive) offset from FP Grows and shrinks during expression evaluation FP Arguments Callee save registers Local variables SP (old FP, RA) °Many variations on stacks possible (up/down, last pushed / next ) °Block structured languages contain link to lexically enclosing frame °Compilers normally keep scalar variables in registers, not memory! High Mem Low Mem

15
ECE 232 L6.Assemb.15 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Compiling a leaf procedure (not nested) °int leaf_example (int g, int h, int i, int j) {int f; f = (g + h) – (i + j); return f; } °Let parameter variables g, h, i, j, correspond to the argument registers $a0, $a1, $a2, $a3. Will use temp. registers $t0= (g + h) and $t1=(i + j). Function f will be stored in $s0. °Steps: °Save the old values of registers ($s0, $t0, $t1) on stack (push) °Issue a jal sub_address instruction ($ra ret_addr, j sub_address) °Perform the computation for $t0, $t1, $s0 using argument registers °Copy the value of f into a return value register $v0 °Restore the old values of the saved registers from stack (pop) °Finally, jump back to the calling routine, jr $ra (PC return_address=PC+4)

16
ECE 232 L6.Assemb.16 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Compiling a leaf procedure, cont’d Leaf_example:# label of the procedure Save the old values of registers ($s0, $t0, $t1) on stack (push) sub $sp, $sp, 12# adjust stack to make room for 3 items sw $t1, 8 ($sp)# save reg $t1 on stack …….# repeat for $t0, $s0 Perform the computation for $t0, $t1, $s0 using argument registers add $t0, $a0, $a1# $t0 g + h add $t1, $a2, $a3# $t1 i + j sub $s0, $t0, $t1# $s0 (g + h) – (i + j) Copy the value of f into a return value register $v0 add $v0, $s0, $zero# returns f ($v0 $s0 + 0) Restore the old values of the saved registers from stack (pop ) lw $s0, 0 ($sp)# restore reg. $s0 for the caller ……. # repeat for $t0, $t1 … add $sp, $sp, 12# adjust the stack to delete 3 items Finally, jump back to the calling routine (PC return address) jr $ra# PC $ra

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google