Title: CS3843
1MIPS Assembly Language
2Why Assembly Language?
- Easy for a human to understand than machine
language.
Adding register 20 and 17 and storing the result
in register 16.
machine language 0x02918020
assembly language add 16, 20, 17
3MIPS Register Set
4MIPS Register Set
- - 31 general-purpose registers
- 1 (zero) register contains the number zero at
all times - There are 32 floating-point registers (f0, f1,
, f31). - They are used in pairs for double precision
numbers.
5Commenting and Starting
- Any text between and the subsequent newline is
considered to be a comment. - In SPIM, program execution begins at the location
with the label main.
This is a comment 03/02/05 main
6Ending of a Program
- syscall
- suspends the execution of the program.
- transfers control to the OS.
- The OS looks at the contents of v0 to determine
what to do. (v0 10 nothing)
This is a comment 03/02/05 main li v0,
10 syscall
7SPIM syscalls
8Using SPIM Simulator
spim . (spim) load program.asm (spim)
run (spim) exit
9Example add2.asm
- Read two numbers from the user add print the
sum exit. - Read an integer
- syscall 5
- Move the contents of v0 to t0
- move t0, v0
- Print an integer
- syscall 1
10Example add2.asm
add2.asm 03/02/05 main li v0, 5
syscall 5 syscall move t0, v0 move the
number to t0 li v0, 5 syscall move t1,
v0 move the number to t1 add t2, t0,
t1 move a0, t2 move the sum to
a0 li v0, 1 syscall 1 syscall li v0,
10 syscall
11Hello World
- Assembler directives
- text segment .text
- The executable part of the program (instructions
to execute) - default
- data segment .data
- Data used by the program
- Everything between a .data directive and the next
.text directive (or the end of the file) is put
into the data segment. - A null-terminated string .asciiz
- A string that is not null-terminated .ascii
12hello.asm
hello.asm 03/02/05 .text main la a0,
hello_msg li v0, 4 syscall li v0,
10 syscall .data hello_msg .asciiz Hello
World\n end of program
13Conditional Execution
- Read two numbers from the user print out the
larger of the two. - branch instructions
14 larger.asm 03/02/05 .text main li v0,
5 get the first number syscall move t0,
v0 li v0, 5 get the second
number syscall move t1, v0 bgt t0, t1,
t0_bigger if t0 t1, goto t0_bigger move t2,
t1 t2 t1 b endif goto
endif t0_bigger move t2, t0 t2
t0 endif move a0, t2 print out
t2 li v0, 1 syscall li v0, 10
exit syscall end of program
15Looping
- Get A from the user.
- Get B from the user. If B
- Set sentinel value S A x B.
- Set multiple m A.
- Loop
- 5.1 Print m.
- 5.2 If m S, then go to the next step.
- 5.3 Otherwise, set m m A, and repeat the
loop. - 6. Terminate.
16Looping
17 multiples.asm 03/02/05 .text main li v0,
5 get the first number syscall move t0,
v0 li v0, 5 get the second
number syscall move t1, v0 blez t1,
exit if B A x B move t3, t0 m A
18loop move a0, t3 print out t3 li v0,
1 syscall beq t2, t3, end_loop if m S,
exit loop add t3, t3, t0 otherwise, m m
A la a0, newline_msg li v0, 4
print_string syscall b loop end_loop exit l
i v0, 10 syscall .data newline_msg .asciiz
\n end of program
19Example palindrome.asm
- S is the starting address of a string.
- A string ends with a newline followed by a 0
character. - xxxxx\n0
- X indicates the character of the address X.
- .space directive
- Allocate n bytes of space in the current segment.
In SPIM, this is only permitted in the data
segment. - lb rt, offset(rd) load a byte from memory
- sign-extended byte is placed in rt.
20- Let A S.
- 2. Let B a pointer to the last character of S.
To find the last - character of S, use the following.
- (a) Let B S.
- (b) Loop
- - If B 0, set B B 2, and continue with
the next step. - - Otherwise, B B 1.
- 3. Loop
- (a) If A B, then the string is a palindrome.
Halt. - (b) If A ? B, then the string is not a
palindrome. Halt. - (c) Set A A 1.
- (d) Set B B 1.
21 palindrome.asm 03/02/05 .text main la a0,
string_space li a1, 1024 li v0, 8
read_string syscall syscall string is read
and placed in string_space la t1,
string_space A S la t2, string_space B
S length_loop lb t3, (t2) load the type
at B into t3 beqz t3, end_length_loop if t3
0, branch addu t2, t2, 1 otherwise, B
B1 b length_loop end_length_loop subu t2,
t2, 2 B B 2 remove \0 and \n
22test_loop bge t1, t2, is_palin lb t3,
(t1) load the byte at A into t3 lb t4,
(t2) load the byte at B into t4 bne t3,
t4, not_palin addu t1, t1, 1 subu t2, t2,
1 b test_loop is_palin la a0,
is_palin_msg li v0, 4 print
is_palin_msg syscall b exit not_palin la a0,
not_palin_msg li v0, 4 print
not_palin_msg syscall b exit
23exit li v0, 10 exit the program syscall .
data string_space .space 1024 is_palin_msg .asci
iz The string is a palindrome.\n not_palin_msg
.asciiz The string is not a palindrome.\n
end of program
24atoi ascii to integer
- Read a line of text from the terminal interpret
it as an integer print it out.
- S pointer to the start of the string, D number
(sum) - Set D 0.
- Loop
- (a) If S \n, then continue with the next
step. - (b) Otherwise,
- (1) S S 1
- (2) D D x 10
- (3) D D ( S 0 )
25 atoi.asm 03/02/05 .text main la a0,
string_space li a1, 1024 li v0, 8
read_string syscall la t0, string_space
Initialize S li t2, 0 sum_loop lb t1,
(t0) load byte at S into t1 addu t0, t0,
1 increment S beq t1, 10, end_sum_loop 10
\n mul t2, t2, 10 t2 t2 x
10 sub t1, t1, 0 t1 t1 0 add t2,
t2, t1 t2 t2 t1
26 b sum_loop end_sum_loop move a0,
t2 li v0, 1 print_int syscall la a0,
newline print \n li v0, 4
print_string syscall exit li v0, 10 exit
the program syscall .data newline .asciiz \n
string_space .space 1024 end of program
27Advanced Topics inMIPS Assembly Language
28Function Calls in MIPS
- The caller must
- Put the parameters into a0 a3. If there are
more than four parameters, the additional
parameters are pushed onto the stack. - Save any of the caller-saved registers which are
used by the caller - Execute a jal (or jalr) to jump to the function.
- jal target jump and link. ra PC4 go to
target. - jarl rd, rs jump and link register.
- rd
29- The callee must
- Create a stack frame by subtracting the frame
size from the stack pointer (sp). - The minimum stack frame size in the MIPS is 32
bytes. - Save any callee-saved registers which are used by
the callee. The frame pointer (fp) must always
be saved. The return address (ra) needs to be
saved only by functions which make function calls
themselves. - Set the frame pointer to the stack pointer the
frame size.
30- The callee then executes the body of the
function. - To return from a function, the callee must
- Put the return value, if any, into register v0.
- Restore callee-saved registers.
- Jump back to ra, using jr instruction.
- jr rs jump register. PC
31- To clean up after a function call, the caller
must - Restore the caller-saved registers.
- If any arguments were passed on the stack
(instead of in a0 a3), pop them off of the
stack. - Extract the return value, if any, from register
v0.
32Example Fibonacci Numbers
- F(n) (where n 0) is
- If n
- Otherwise, F(n) F(n-1) F(n-2).
- Recursive algorithm
33fib-s.asm
Fibonacci Numbers - recursive functions
03/04/05 .text main subu sp,
sp, 32 sw ra, 28(sp) sw
fp, 24(sp) addu fp, sp,
32 Get number n from the user, put
into a0 li v0, 5 syscall
move a0, v0 jal fib
jump and link a0 PC4, goto fib
fib(n) calculated, print it out move
a0, v0 v0 holds the return value
li v0, 1 print_int syscall
34 la a0, newline li v0, 4
syscall newline
li v0, 10 exit syscall
fibonacci fib subu sp, sp, 32
sw ra, 28(sp) sw fp,
24(sp) sw s0, 20(sp)
sw s1, 16(sp) sw s2,
12(sp) addu fp, sp, 32
move s0, a0 get new n from the
caller blt s0, 2, fib_base_case
if n
35 sub a0, s0, 1 compute fib (n -
1) jal fib move s1,
v0 s1 fib (n - 1) sub
a0, s0, 2 compute fib (n - 2)
jal fib move s2, v0
s2 fib (n - 2) add
v0, s1, s2 v0 fib(n-1) fib(n-2)
b fib_return fib_base_case
li v0, 1 fib(0)
fib(1) 1
36fib_return lw ra, 28(sp) lw
fp, 24(sp) lw s0, 20(sp)
lw s1, 16(sp) lw
s2, 12(sp) addu sp, sp, 32
jr ra .data newline
.asciiz "\n" end of program