100 likes | 231 Views
MIPS Assembly. Saleh Dindar CDA3101 Fall 2014. mul $t0, $a0, $a1 add $v0, $t0, $a2. Problem 1: Linear function. x → $a0 a → $a1 b → $a2 y → $v0. lw $t0, 0($a0) lw $t1, 0($a1) sw $t1, 0 ($a0 ) sw $t0, 0 ($a1 ). Problem 2: Swap numbers.
E N D
MIPS Assembly Saleh Dindar CDA3101 Fall 2014
mul$t0, $a0, $a1 add $v0, $t0, $a2 Problem 1: Linear function x→ $a0 a →$a1 b → $a2 y→ $v0
lw$t0, 0($a0) lw$t1, 0($a1) sw$t1, 0($a0) sw$t0, 0($a1) Problem 2: Swap numbers Assume a and b hold addresses to 32-bit integers in memory. Swap the numbers. a → $a0 b → $a1
slt $t0, $a0, $zero bne$t0, $zero, elsepart move $v0, $a0 j endif elsepart: sub $v0, $zero, $a0 endif: Problem 3: Absolute value x → $a0 y→ $v0
li $v0, 1 loop: beq $a0, $zero, endloop mul $v0, $a0, $v0 addi $a0, $a0, -1 j loop endloop: Problem 4: Factorial n→ $a0 y→ $v0
move $v0, $zero move $t0, $zero loop: beq $t0, $a0, endloop lw $t1, 0($a1) add $v0, $v0, $t1 addi $t0, $t0, 1 addi $a1, $a1, 4 j loop endloop: Problem 5: Sum of array n → $a0 A→ $a1 y→ $v0 k → $t0
move $t0, $zero li $t1, 1 li $t2, 1 loop: beq $t0, $a0, endloop add $t3, $t1, $t2 move $t1, $t2 move $t2, $t3 addi $t0, $t0, 1 j loop endloop: move $v0, $t1 Problem 6: Fibonacci numbers n → $a0 y→ $v0
SPIM Simulator • Download from http://sourceforge.net/projects/spimsimulator/files/ • QtSpim_9.1.12_Windows.exe • QtSpim_9.1.13.mac.mpkg.zip • Read appendix A of the textbook
.text 0x400000 mul$t0, $a0, $a1 add $v0, $t0, $a2 .word 0,0,0,0,0,0 Problem 1 in SPIM .text : Address in memory to load the program .word Put values in memory