MIPS Instructions and System Calls for CPU Management
Learn about MIPS ISA instructions, CPU cycles, memory operations, and system calls for efficient CPU management in assembly programming.
MIPS Instructions and System Calls for CPU Management
E N D
Presentation Transcript
CS 136 Lab 2 MIPS ISA SPIM
Prob 2.30 sll $a2, $a2, 2 sll $a3, $a3, 2 add $v0, $zero, $zero add $t0, $zero, $zero outer: add $t4, $a0, $t0 lw $t4, 0($t4) add $t1, $zero, $zero inner: add $t3, $a1, $t1 lw $t3, 0($t3) bne $t3, $t4, skip addi $v0, $v0, 1 skip: addi $t1, $t1, 4 bne $t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer
Prob 2.31 • add, addi, sll => 1 cycle • lw, bne => 2 cycles • For 2GHz CPU, how much time for 1 cycle.
Prob 2.33 • x[4] = x[5] + a; • Base addr for X = 6,400,000ten • a is stored in $t3 • MIPS instruction (s)?
Prob 2.36 • for (i=0; i<=100; i=i+1) {a[i] = b[i] + c;} • How many memory data references will be made during execution? • load, store • How many instructions are executed during the running of this code?
Prob 2.39 • Suppose • lb $s0, 100($zero) #byte@100= 0x0F? • lb $s1, 200($zero) #byte@200= 0xFF • What are the values of $s0 and $s1?
Exercise 1 move $t1, $v0 add $t2, $t0, $t1 #print out str3 and the sum #code start #code end .data str1: .asciiz "input first integer:" str2: .asciiz "input second integer:" str3: .asciiz "the sum is:" .text .globl main main: li $v0, 4 la $a0, str1 syscall li $v0, 5 syscall move $t0, $v0 #print out str2 #input second integer #move the input to $t1 #code start #code end
General Purpose Registers (32) • Reference: A-24 • $at (1), $k0 (26), and $k1 (27): reserved for the assembler and operating system. • $a0–$a3 (4–7) are used to pass the first four arguments to routines. • $v0(2) and $v1(3) are used to return values from functions. • What else usage?
General Purpose Registers (32) • Registers $t0–$t9 (8–15, 24, 25) are caller-saved registers that are used to hold temporary quantities that need not be preserved across calls. • Registers $s0–$s7 (16–23) are callee-saved registers that hold long-lived values that should be preserved across calls. • $gp (28), $sp (29), $fp (30), $ra (31) • PCSPIM…
Source code format .text .globl main main: [#comment] [label:] opcode [op1],[op2],[op3] .data str: .asciiz “string” integer: .word 0xaf,0xffff2,0x4233 … Reference: A-47
Load and Store li $t0, 5 #load immediate la $t0, str #load address lw $t0, 8($sp) #load word lb $t0, ($s1) #load byte sb ‘a’, ($s0) #store byte sw $a0, 100($sp) #store word
Prob 2.39 test code .text .globl main main: lb $s0, bb1 lb $s1, bb2 .data bb1: .byte 0x0F bb2: .byte 0xFF
Arithmetic Instruction add $t2, $t1, $t0 # $t2 = $t1 + $t0 addi $t1, $t0, 5 # $t1 = $t0 + 5 sub $t2, $t1, $t0 # $t2 = $t1 - $t0 mul $t2, $t1, $t0 # $t2 = $t1 * $t0 div $t2, $t1, $t0 # $t2 = $t1 / $t0 • If we multiply two 32 bit unsigned integers… what is the size in bits of the largest possible result?
#calculate W=A*X^2+B*X+C #asnser should be 180 .text .globl main main: #code start #code end .data X: .word 7 A: .word 3 B: .word 4 C: .word 5 W: .word 0 ans: .asciiz "answer = "
pseudo-instruction • Instructions provided by an assembler but not implemented in hardware. • li $t0, 0x40044005 • lui $t0, 4004 • ori $t0, $t0, 4005 • li $t0, 0x4005 • ori $t0, $t0, 4005 • move $t1, $t2 • mul $t2, $t2, $t3 • mult $t4, $t1 • mflo $t4 • A-51
Branches • j <addr> • beq $t0, $t1, <addr> • bne $t0, $t1, <addr> • slt $t1, $t2, $t3 • if (t2<t3) then t1=1, else t1=0 • slti $t1, $t2, 7 • if (t2<7) then t1=1, else t1=0
pseudo-instruction • beq $t1, small, L • li $at, small • beq $t1, $at, L • beq $t2, big, L • li $at, big • beq $at, $t2, L • blt $t0, $t1, L • slt $at, $t0, $t1 • bne $at, $zero, L • ble $t3, $t5, L • slt $at, $t5, $t3 • beq $at, $zero, L
Branch Example .text .globl main main: la $t2, str li $t1, 0 nextch: lb $t0, ($t2) beqz $t0, strend add $t1, $t1, 1 add $t2, 1 j nextch strend: move $a0, $t1 li $v0, 1 syscall .data str: .asciiz "hello world!"
Exercise .text .globl main main: #swap all of ‘a’ from str to ‘A’ #code begin #code end strend: la $a0, str li $v0, 4 syscall .data str: .asciiz "aabbbababababbaaaabbaa"
void main() { char str[] = "aabbbababababbaaaabbaa"; char* t2 = str; char t1 = 'A'; char t0; do { t0 = *t2; if(t0 == 0) break; if(t0 == 'a') *t2 = t1; t2++; }while(1); printf("%s\n", str); }
Exercise .text .globl main main: #load the address of string to $t2 #load ‘A’ to $t1 nextch: #load byte from address ($t2) to $t0 #branch to strend if $t0 = 0 #branch to nota if $t0 != 'a' #store byte $t1 to address ($t2) nota: increment $t2 by 1 jump to nextch strend: la $a0, str li $v0, 4 syscall .data str: .asciiz "aabbbababababbaaaabbaa"