Presentation is loading. Please wait.

Presentation is loading. Please wait.

MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded.

Similar presentations


Presentation on theme: "MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded."— Presentation transcript:

1 MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded text passage

2 MicroComputer Engineering IntroLab1 page 2 Lab 1 Procedure calls. Byte and word array indexing, pointers. Conditional branch instructions. Simple loops. Reading/ writing from/to data memory.

3 MicroComputer Engineering IntroLab1 page 3 Branch instruction (part of) Beqrs rt label Bners rt label Together with Slt-instruction and $0 enough for first lab.

4 MicroComputer Engineering IntroLab1 page 4 Register conventions (fig A9) Reg. nameNrUsage zero 0Constant 0 at1Reserved for assembler v0 - v12-3Expr. evaluation, function results a0 - a34-7Argument 1-4 t0 - t78-15Temporary (not saved) s0 - s716-23Saved Temporary t8 - t924-25Temporary (not saved) k0 - k126-27Reserved for OS kernel gp28Pointer to global area sp29Stack pointer fp30Frame pointer ra31Return address

5 MicroComputer Engineering IntroLab1 page 5 Lab 1 register usage Use only $sxfor accessing global variables $txfor your local variables $axfor parameters $vxfor results $r0(=$0)

6 MicroComputer Engineering IntroLab1 page 6 Lab 1 register usage Use only $sxfor accessing global variables $txfor your local variables $axfor parameters $vxfor results $r0(=$0)

7 MicroComputer Engineering IntroLab1 page 7 Lab 1 - stack frame ============== "sum" with frame pointer ================ ===== example of a subroutine with frame pointer. ===== ===== easier to restore and manage the stack. ===== ===== little bit less efficient code. ===== # ================================== # compute sum = n + (n-1)+(n-2)+...+ 1 # call only for n 0. # sum( n: int ): int (direct-recursive) # x: int; (example of local variable) # if n = 1 # then return( 1 ) # else return( n + sum(n-1) ); sum: addiu $sp, $sp, -4 sw $31, 0($sp)# push the return addr addiu $sp, $sp, -4 sw $fp, 0($sp)# push the old frame pointer move $fp, $sp# establish new frame pointer. addiu $sp, $sp, -8# make room for 2 full # word local variables # everything above is called the "entry sequence". # the stack now looks like this. # YOU MUST SHOW US A DIAGRAM LIKE THIS, FOR ALL # THE SUBROUTINES YOU WRITE IN YOUR FIRST LAB. # # +-------------------+ # | uninit, for "x" | -8($fp) <= $sp points here # +-------------------+ # | uninit, for "n" | -4($fp) # +-------------------+ # | old frame pointer | 0($fp) <= $fp points here # +-------------------+ # | our return addr | 4($fp) # --+-------------------+-- # | | # | caller's stack | # | | # # (we don't need "x" below, just given as an example.) # now this activation can access its activation record # RELATIVE TO THE FRAME POINTER, like this: save: sw $a0, -4($fp)# save "n" into local var if: ori $t0, $zero, 1# $t0 := 1 bne $a0, $t0, else# n 1, jump to "else" then: ori $v0, $zero, 1# n = 1: return value := 1, b exit# escape to exit sequence. else: addiu $a0, $a0, -1# compute n-1 (destroys $a0). bal sum# $v0 := sum( n-1 ). lw $t0, -4($fp)# fetch value "n". addu $v0, $v0, $t0# $v0 := n + sum( n-1 ). # the exit sequence reverses the entry sequence: exit: move $sp, $fp# all local var's gone: easy ! lw $fp, 0($sp)# restore old $fp addiu $sp, $sp, 4# and pop that word, lw $31, 0($sp)# restore return address, addiu $sp, $sp, 4# and pop that word. jr $31# return =========== end "sum" with frame pointer ===============


Download ppt "MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded."

Similar presentations


Ads by Google