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

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

Embed Size (px)

Citation preview

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

MicroComputer Engineering IntroLab1 page 1

Introduction Lab1

A crash course in assembler programming

Learn how a processor works! Decode a coded text passage

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

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.

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

MicroComputer Engineering IntroLab1 page 3

Branch instruction (part of)

Beq rs rt label Bne rs rt label

Together with Slt-instruction and $0

enough for first lab.

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

MicroComputer Engineering IntroLab1 page 4

Register conventions (fig A9)

Reg. name Nr Usage zero 0 Constant 0 at 1 Reserved for assembler v0 - v1 2-3 Expr. evaluation, function

results a0 - a3 4-7 Argument 1-4 t0 - t7 8-15 Temporary (not saved) s0 - s7 16-23 Saved Temporary t8 - t9 24-25 Temporary (not saved) k0 - k1 26-27 Reserved for OS kernel gp 28 Pointer to global area sp 29 Stack pointer fp 30 Frame pointer ra 31 Return address

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

MicroComputer Engineering IntroLab1 page 5

Lab 1 register usage

Use only

$sx for accessing global variables

$tx for your local variables

$ax for parameters

$vx for results

$r0 (=$0)

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

MicroComputer Engineering IntroLab1 page 6

Lab 1 register usage

Use only

$sx for accessing global variables

$tx for your local variables

$ax for parameters

$vx for results

$r0 (=$0)

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

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 ===============