CL-SPIM Version 5.5 of August 12, 1994 Copyright 1990-1994 by James R. Larus (larus@cs.wisc.edu). Copyright (C) 1991-1994 by Anne Rogers (amr@cs.princeton.edu) and Scott Rosenberg (scottr@cs.princeton.edu). All Rights Reserved. See the file README a full copyright notice. Loaded: /home/l/i/lib51/lib/trap.handler (cl-spim) #### Assembly Language Examples (cl-spim) #### (cl-spim) #### File: assembly-language.in (cl-spim) #### Author: CS 51 (Bob Walton) (cl-spim) #### Version: 1 (cl-spim) (cl-spim) load "assembly_language.asm" (cl-spim) (cl-spim) # (cl-spim) (cl-spim) # Immediate Instructions (cl-spim) (cl-spim) . addi $t0, $0, 0x555 # $t0 = 0x555 [0x004000c4] addi $8, $0, 1365 $08 = 0x00000555 (cl-spim) (cl-spim) . addi $t1, $t0, 0x333 # $t1 = $t0 + 0x333 [0x004000c8] addi $9, $8, 819 $09 = 0x00000888 (cl-spim) (cl-spim) . slti $t1, $t0, 0x333 # $t1 = ($t0 < 0x333) [0x004000cc] slti $9, $8, 819 $09 = 0x00000000 (cl-spim) (cl-spim) . slti $t1, $t0, 0x666 # $t1 = ($t0 < 0x666) [0x004000d0] slti $9, $8, 1638 $09 = 0x00000001 (cl-spim) (cl-spim) . ori $t1, $t0, 0x333 # $t1 = $t0 | 0x333 [0x004000d4] ori $9, $8, 819 $09 = 0x00000777 (cl-spim) (cl-spim) . andi $t1, $t0, 0x333 # $t1 = $t0 & 0x333 [0x004000d8] andi $9, $8, 819 $09 = 0x00000111 (cl-spim) (cl-spim) . xori $t1, $t0, 0x333 # $t1 = $t0 ^ 0x333 [0x004000dc] xori $9, $8, 819 $09 = 0x00000666 (cl-spim) (cl-spim) # (cl-spim) (cl-spim) # More Immediate Instructions (cl-spim) (cl-spim) . lui $t1, 0x1234 # $t1 = 0x12340000 [0x004000e0] lui $9, 4660 $09 = 0x12340000 (cl-spim) (cl-spim) . lui $t0, 0x7fff # $t0 = 0x7fff0000 [0x004000e4] lui $8, 32767 $08 = 0x7fff0000 (cl-spim) (cl-spim) . ori $t0, $t0, 0xffff # $t0 = 0xffffffff [0x004000e8] ori $8, $8, -1 $08 = 0x7fffffff (cl-spim) (cl-spim) . addu $t1, $t0, 1 # $t1 = $t0 + 1 unsigned [0x004000ec] addiu $9, $8, 1 $09 = 0x80000000 (cl-spim) (cl-spim) . add $t1, $t0, 1 # $t1 = $t0 + 1 signed [0x004000f0] addi $9, $8, 1 ----- Exception occurred at PC=0x004000f0 Arithmetic overflow --------------------------------------> PC = 80000080 (cl-spim) (cl-spim) . lui $t0, 0x8000 # $t0 = 0x80000000 [0x004000f4] lui $8, -32768 $08 = 0x80000000 (cl-spim) (cl-spim) . slti $t1, $t0, 1 # $t1 = ($t0 < 1) signed [0x004000f8] slti $9, $8, 1 $09 = 0x00000001 (cl-spim) (cl-spim) . sltiu $t1, $t0, 1 # $t1 = ($t0 < 1) uns. [0x004000fc] sltiu $9, $8, 1 $09 = 0x00000000 (cl-spim) (cl-spim) # (cl-spim) (cl-spim) # Register Instructions (cl-spim) (cl-spim) . addi $t0, $0, 0x555 # $t0 = 0x555 [0x00400100] addi $8, $0, 1365 $08 = 0x00000555 (cl-spim) (cl-spim) . addi $t1, $0, 0x333 # $t1 = 0x333 [0x00400104] addi $9, $0, 819 $09 = 0x00000333 (cl-spim) (cl-spim) . add $t2, $t0, $t1 # $t2 = $t0 + $t1 [0x00400108] add $10, $8, $9 $10 = 0x00000888 (cl-spim) (cl-spim) . sub $t2, $t0, $t1 # $t2 = $t0 - $t1 [0x0040010c] sub $10, $8, $9 $10 = 0x00000222 (cl-spim) (cl-spim) . slt $t2, $t0, $t1 # $t2 = ( $t0 < $t1 ) [0x00400110] slt $10, $8, $9 $10 = 0x00000000 (cl-spim) (cl-spim) . addi $t2, $0, 0x666 # $t2 = 0x666 [0x00400114] addi $10, $0, 1638 $10 = 0x00000666 (cl-spim) (cl-spim) . slt $t2, $t0, $t2 # $t2 = ( $t0 < $t2 ) [0x00400118] slt $10, $8, $10 $10 = 0x00000001 (cl-spim) (cl-spim) . or $t2, $t0, $t1 # $t2 = $t0 | $t1 [0x0040011c] or $10, $8, $9 $10 = 0x00000777 (cl-spim) (cl-spim) . and $t2, $t0, $t1 # $t2 = $t0 & $t1 [0x00400120] and $10, $8, $9 $10 = 0x00000111 (cl-spim) (cl-spim) . xor $t2, $t0, $t1 # $t2 = $t0 ^ $t1 [0x00400124] xor $10, $8, $9 $10 = 0x00000666 (cl-spim) (cl-spim) # (cl-spim) # More Register Instructions (cl-spim) (cl-spim) . lui $t0, 0x7fff # $t0 = 0x7fff0000 [0x00400128] lui $8, 32767 $08 = 0x7fff0000 (cl-spim) . ori $t0, $t0, 0xffff # $t0 = 0x7fffffff [0x0040012c] ori $8, $8, -1 $08 = 0x7fffffff (cl-spim) (cl-spim) . addi $t1, $0, 1 # $t1 = 1 [0x00400130] addi $9, $0, 1 $09 = 0x00000001 (cl-spim) (cl-spim) . addu $t2, $t0, $t1 # $t2 = $t0 + $t1 uns [0x00400134] addu $10, $8, $9 $10 = 0x80000000 (cl-spim) (cl-spim) . add $t2, $t0, $t1 # $t2 = $t0 + $t1 signed [0x00400138] add $10, $8, $9 ----- Exception occurred at PC=0x00400138 Arithmetic overflow --------------------------------------> PC = 80000080 (cl-spim) (cl-spim) . lui $t0, 0x8000 # $t0 = 0x80000000 [0x0040013c] lui $8, -32768 $08 = 0x80000000 (cl-spim) (cl-spim) . subu $t2, $t0, $t1 # $t2 = $t0 - $t1 uns [0x00400140] subu $10, $8, $9 $10 = 0x7fffffff (cl-spim) (cl-spim) . sub $t2, $t0, $t1 # $t2 = $t0 - $t1 signed [0x00400144] sub $10, $8, $9 ----- Exception occurred at PC=0x00400144 Arithmetic overflow --------------------------------------> PC = 80000080 (cl-spim) (cl-spim) . slt $t2, $t0, $t1 # $t2 = ($t0 < $t1) signed [0x00400148] slt $10, $8, $9 $10 = 0x00000001 (cl-spim) (cl-spim) . sltu $t2, $t0, $t1 # $t2 = ( $t0 < $t1 ) uns [0x0040014c] sltu $10, $8, $9 $10 = 0x00000000 (cl-spim) # (cl-spim) (cl-spim) # Example Loop (cl-spim) (cl-spim) br loop_example # in assembly_language.asm (cl-spim) (cl-spim) run loop_example Breakpoint encountered at 0x00400024 # int $t0 = 1; # do $t0 = $t0 + $t0 while ( $t0 < 8 ); .globl loop_example loop_example: addi $t0, $0, 1 # $t0 = 1 (cl-spim) (cl-spim) s 10000 [0x00400024] addi $8, $0, 1 $08 = 0x00000001 loop: add $t0, $t0, $t0 # $t0 = $t0 + $t0 [0x00400028] add $8, $8, $8 $08 = 0x00000002 slti $t1, $t0, 8 # $t1 = ( $t0 < 8 ) [0x0040002c] slti $9, $8, 8 $09 = 0x00000001 bne $t1, $0, loop # if ( $t1 != 0 ) [0x00400030] bne $9, $0, -8 [loop] --------------------------------------> PC = 00400028 loop: add $t0, $t0, $t0 # $t0 = $t0 + $t0 [0x00400028] add $8, $8, $8 $08 = 0x00000004 slti $t1, $t0, 8 # $t1 = ( $t0 < 8 ) [0x0040002c] slti $9, $8, 8 $09 = 0x00000001 bne $t1, $0, loop # if ( $t1 != 0 ) [0x00400030] bne $9, $0, -8 [loop] --------------------------------------> PC = 00400028 loop: add $t0, $t0, $t0 # $t0 = $t0 + $t0 [0x00400028] add $8, $8, $8 $08 = 0x00000008 slti $t1, $t0, 8 # $t1 = ( $t0 < 8 ) [0x0040002c] slti $9, $8, 8 $09 = 0x00000000 bne $t1, $0, loop # if ( $t1 != 0 ) [0x00400030] bne $9, $0, -8 [loop] # goto loop addi $v0, $0, 10 # exit() [0x00400034] addi $2, $0, 10 $02 = 0x0000000a syscall (cl-spim) . (cl-spim) (cl-spim) # (cl-spim) (cl-spim) # Example Using SYSCALL (cl-spim) (cl-spim) br syscall_example # in assembly_language.asm (cl-spim) (cl-spim) run syscall_example Breakpoint encountered at 0x0040003c # $t0 = read_int(); # $t0 = $t0 + read_int(); # print ( $t0 ); .globl syscall_example syscall_example: # $t0 = read_int(); # addi $v0, $0, 5 # $v0 = 5 (cl-spim) (cl-spim) s 10000 [0x0040003c] addi $2, $0, 5 $02 = 0x00000005 syscall # $v0 = read_int() 333333333 [0x00400040] syscall add $t0, $0, $v0 # $t0 = $v0 [0x00400044] add $8, $0, $2 $08 = 0x13de4355 # $t0 = $t0 + read_int(); # addi $v0, $0, 5 # $v0 = 5 [0x00400048] addi $2, $0, 5 $02 = 0x00000005 syscall # $v0 = read_int() 555555555 [0x0040004c] syscall add $t0, $t0, $v0 # $t0 = $t0 + $v0 [0x00400050] add $8, $8, $2 $08 = 0x34fb5e38 # print_int ( $t0 ) # addi $v0, $0, 1 # $v0 = 1 [0x00400054] addi $2, $0, 1 $02 = 0x00000001 add $a0, $0, $t0 # $a0 = $t0 [0x00400058] add $4, $0, $8 $04 = 0x34fb5e38 syscall # print_int ( $a0 ) 888888888[0x0040005c] syscall addi $v0, $0, 10 # exit() [0x00400060] addi $2, $0, 10 $02 = 0x0000000a syscall (cl-spim) . (cl-spim) (cl-spim) # (cl-spim) (cl-spim) # Example Subroutine Call (cl-spim) (cl-spim) br subroutine_call_example (cl-spim) (cl-spim) run subroutine_call_example Breakpoint encountered at 0x00400068 # $a0 = read_int(); # $a1 = read_int(); # $v0 = max ( $a0, $a1 ); # print ( $v0 ); .globl subroutine_call_example subroutine_call_example: # $a0 = read_int(); # addi $v0, $0, 5 # $v0 = 5 (cl-spim) (cl-spim) s 10000 [0x00400068] addi $2, $0, 5 $02 = 0x00000005 syscall # $v0 = read_int() 333333333 [0x0040006c] syscall add $a0, $0, $v0 # $a0 = $v0 [0x00400070] add $4, $0, $2 $04 = 0x13de4355 # $a1 = read_int(); # addi $v0, $0, 5 # $v0 = 5 [0x00400074] addi $2, $0, 5 $02 = 0x00000005 syscall # $v0 = read_int() 555555555 [0x00400078] syscall add $a1, $0, $v0 # $a1 = $v0 [0x0040007c] add $5, $0, $2 $05 = 0x211d1ae3 # $v0 = max ( $a0, $a1 ) # jal max [0x00400080] jal 0x00400098 [max] --------------------------------------> PC = 00400098 ## The max function: ## ## int max ( int x, int y ) ## { if ( x < y ) return y; else return x; } ## .globl max max: # if ( x < y ) goto a0_lt_a1; # slt $t0, $a0, $a1 # t0 = ( $a0 < $a1 ) [0x00400098] slt $8, $4, $5 $08 = 0x00000001 bne $t0, $0, a0_lt_a1 # if ( $t0 != 0 ) [0x0040009c] bne $8, $0, 12 [a0_lt_a1] --------------------------------------> PC = 004000a8 # return y; # a0_lt_a1: add $v0, $0, $a1 # $v0 = $a1 [0x004000a8] add $2, $0, $5 $02 = 0x211d1ae3 jr $ra # return [0x004000ac] jr $31 --------------------------------------> PC = 00400084 # print_int ( $v0 ) # add $a0, $0, $v0 # $a0 = $v0 [0x00400084] add $4, $0, $2 $04 = 0x211d1ae3 addi $v0, $0, 1 # $v0 = 1 [0x00400088] addi $2, $0, 1 $02 = 0x00000001 syscall # print_int ( $a0 ) 555555555[0x0040008c] syscall addi $v0, $0, 10 # exit() [0x00400090] addi $2, $0, 10 $02 = 0x0000000a syscall (cl-spim) . (cl-spim) (cl-spim) # (cl-spim) (cl-spim) # Example Print String (cl-spim) (cl-spim) br print_string_example (cl-spim) (cl-spim) run print_string_example Breakpoint encountered at 0x004000b0 # print_string ("HI\n"); .globl print_string_example print_string_example: .data string: .ascii "HI\n\0" .text addi $v0, $0, 4 (cl-spim) (cl-spim) s 10000 [0x004000b0] addi $2, $0, 4 $02 = 0x00000004 la $a0, string [0x004000b4] lui $4, 4097 [string] $04 = 0x10010000 syscall HI [0x004000b8] syscall addi $v0, $0, 10 # exit() [0x004000bc] addi $2, $0, 10 $02 = 0x0000000a syscall (cl-spim) . (cl-spim) (cl-spim) # (cl-spim) (cl-spim) # Data Examples (cl-spim) (cl-spim) . .data (cl-spim) (cl-spim) . label1: (cl-spim) p label1 Data seg @ 0x10010004 (268500996) = 0x00000000 (0) (cl-spim) . (cl-spim) . .ascii "HELLO\n\0" (cl-spim) (cl-spim) . label2: (cl-spim) p label2 Data seg @ 0x1001000b (268501003) = 0x00000000 (0) (cl-spim) . (cl-spim) . .align 2 (cl-spim) (cl-spim) . label3: (cl-spim) p label3 Data seg @ 0x1001000c (268501004) = 0x00000000 (0) (cl-spim) . (cl-spim) . .word 0x1111 (cl-spim) (cl-spim) . label4: (cl-spim) p label4 Data seg @ 0x10010010 (268501008) = 0x00000000 (0) (cl-spim) . (cl-spim) p label3 Data seg @ 0x1001000c (268501004) = 0x00001111 (4369) (cl-spim) . (cl-spim) . .text (cl-spim) (cl-spim) # (cl-spim) (cl-spim) . lw $t0, label3 [0x00400150] lui $1, 4097 [label3] $01 = 0x10010000 [0x00400154] lw $8, 12($1) [label3] $08 = 0x00001111 (cl-spim) (cl-spim) . addi $t0, $t0, 0x2222 [0x00400158] addi $8, $8, 8738 $08 = 0x00003333 (cl-spim) (cl-spim) . sw $t0, label3 [0x0040015c] lui $1, 4097 [label3] $01 = 0x10010000 [0x00400160] sw $8, 12($1) [label3] (cl-spim) (cl-spim) p label3 Data seg @ 0x1001000c (268501004) = 0x00003333 (13107) (cl-spim) . (cl-spim) . lw $t0, label3 [0x00400164] lui $1, 4097 [label3] $01 = 0x10010000 [0x00400168] lw $8, 12($1) [label3] $08 = 0x00003333 (cl-spim) (cl-spim) # (cl-spim) (cl-spim) # Multiply Instructions (cl-spim) (cl-spim) . addi $t0, $0, 0x1000 # $t0 = 0x1000 [0x0040016c] addi $8, $0, 4096 $08 = 0x00001000 (cl-spim) (cl-spim) . multu $t0, $t0 # $hi$lo = $t0 * $t0 [0x00400170] multu $8, $8 (cl-spim) # unsigned (cl-spim) (cl-spim) . mflo $t1 # $t1 = $lo [0x00400174] mflo $9 $09 = 0x01000000 (cl-spim) (cl-spim) . addi $t2, $0, 0x7654 # $t2 = 0x7654 [0x00400178] addi $10, $0, 30292 $10 = 0x00007654 (cl-spim) (cl-spim) . multu $t1, $t2 # $hi$lo = $t1 * $t2 [0x0040017c] multu $9, $10 (cl-spim) # unsigned (cl-spim) (cl-spim) . mfhi $t3 # $t3 = $hi [0x00400180] mfhi $11 $11 = 0x00000076 (cl-spim) (cl-spim) . mflo $t4 # $t4 = $lo [0x00400184] mflo $12 $12 = 0x54000000 (cl-spim)