MIPS coding slt slti slt t 3 t

  • Slides: 18
Download presentation
MIPS coding

MIPS coding

slt, slti • slt $t 3, $t 1, $t 2 – set $t 3

slt, slti • slt $t 3, $t 1, $t 2 – set $t 3 to be 1 if $t 1 < $t 2; else clear $t 3 to be 0. – “Set Less Than. ” • slti $t 3, $t 1, 100 – set $t 3 to be 1 if $t 1 < 100; else clear $t 3 to be 0.

Using slt $t 3, bne $t 3, ori $t 0, j L 22 L

Using slt $t 3, bne $t 3, ori $t 0, j L 22 L 21: ori $t 0, L 22: $t 1, $t 2 $zero, L 21 $t 1, 0 $t 2, 0

Complete MIPS code • The text segment in the source code usually starts with.

Complete MIPS code • The text segment in the source code usually starts with. text. globl main: where ``main’’ is the label associated with the address of the first instruction of the code. • And the code usually ends with li $v 0, 10 # telling the simulator to stop syscall • Comment with `#’

The if-else code. text. globl main: # if ($t 1 >= $t 2) $t

The if-else code. text. globl main: # if ($t 1 >= $t 2) $t 0 = $t 1; else $t 0 = $t 2; li $t 0, 0 li $t 1, 16 li $t 2, 11 L 1: L 2: done: sub $t 3, $t 1, $t 2 srl $t 3, 31 bne $t 3, $zero, L 1 or $t 0, $t 1, $0 j L 2 or $t 0, $t 2, $0 li $v 0, 10 syscall

SPIM • Run codes with SPIM is a simulator. – Use any editor to

SPIM • Run codes with SPIM is a simulator. – Use any editor to write the source file, save it as an. asm file. – Run SPIM, load the source file. – F 10 to step through the code. Monitor how the registers change. – F 5 to run the code – Can set breakpoints for debugging • SPIM can be downloaded at http: //spimsimulator. sourceforge. net/ Lots of good references online, like https: //www. cs. tcd. ie/~waldroj/itral/spim_ref. html

Working with the simulator • Can check – How the program runs – How

Working with the simulator • Can check – How the program runs – How the instructions are encoded, addressed – How to monitor the change of the registers – Later, how the memory is used to store data

Some Comments • Being able to write if-else, we can have all other fancy

Some Comments • Being able to write if-else, we can have all other fancy things like for loop, while loop…. • That is why we do not have an instruction for the for loop or while loop, but we build it from the if-else.

Loop • A loop is needed when we need to do something repeatedly •

Loop • A loop is needed when we need to do something repeatedly • It must have – A loop body (what you should do in each iteration) – A checking point to see if we still need to go on – A mechanism to bring us from the end of the loop back to the beginning of the loop (therefore it is called a loop)

Loop example 1 • How to count the number of 1 s in a

Loop example 1 • How to count the number of 1 s in a register?

Counting the number of 1 s. text. globl main: LOOP: done: # counting the

Counting the number of 1 s. text. globl main: LOOP: done: # counting the number of 1 s in register $s 0, no need to save it li $s 0, 97 li $t 0, 0 andi $t 1, $s 0, 1 add $t 0, $t 1 srl $s 0, 1 bne $s 0, $zero, LOOP li $v 0, 10 syscall

Loop example 2 • How to translate the following to MIPS assembly? – We

Loop example 2 • How to translate the following to MIPS assembly? – We first translate into a C program using if and goto 5/24/2021 week 04 -3. ppt 12

Compiling a while loop in C • Assume that i and k correspond to

Compiling a while loop in C • Assume that i and k correspond to registers $s 3 and $s 5 and starting address of array save is in $s 6 5/24/2021 week 04 -3. ppt 13

Compiling a while loop in C • Assume that i and k correspond to

Compiling a while loop in C • Assume that i and k correspond to registers $s 3 and $s 5 and starting address of array save is in $s 6 5/24/2021 week 04 -3. ppt 14

While Loop • How many instructions will be executed for the following array save?

While Loop • How many instructions will be executed for the following array save? – Assume that k = 10 and i = 0 initially 5/24/2021 week 04 -3. ppt 15

Optimized • How many instructions now? – Assume k = 10 and i =

Optimized • How many instructions now? – Assume k = 10 and i = 0 initially 5/24/2021 week 04 -3. ppt 16

The loop code. data save: . word 10, 10, 10, 11, 12, . text.

The loop code. data save: . word 10, 10, 10, 11, 12, . text. globl main: li $s 3, 0 li $s 5, 10 la $s 6, save Loop: sll $t 1, $s 3, 2 add $t 1, $s 6 lw $t 0, 0($t 1) bne $t 0, $s 5, Exit addi $s 3, 1 j Loop Exit: done: li $v 0, 10 # these two lines are to tell the simulator to stop syscall

Data segment and code segment • The code has a data segment and a

Data segment and code segment • The code has a data segment and a code (text) segment. • The beginning of the data segment in the assembly source code is indicated as. data and followed by several declarations such as – A: . word 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 meaning an array of words whose starting address is associated with label ``A. ’’ – Several notes: • It will allocate continuous spaces in the memory for the data • . word means everything is 4 bytes • save: is a label associated with the address of the first byte allocated. Like the label for the instructions, label for an address is also an address.