Today's Topics: Procedures More Examples MARS Intro Number Systems
Today's Topics: Procedures More Examples MARS Intro Number Systems
• Today’s topics:
Procedures
More examples
MARS intro
Number systems
1
Instructions for Making Decisions
• Example: if (i==j) h = i + j;
• bne $s0, $s1, Lbl1
add $s3, $s0, $s1
Lbl1: ...
2
Another Instruction for Changing Flow
• Example: if (i!=j)
h=i+j; else
h=i-j;
beq $s0, $s1, Else
add $s3, $s0, $s1
j Exit
Else: sub $s3, $s0, $s1
Exit: ...
3
Compiling While Loops
4
More Instructions for Making Decisions
5
More Instructions for Making Decisions
6
Other Branch Instructions
7
Case (Switch) Statement
Such shifts are called logical because they fill with zeros
10
Compiling a While Loop
• Compile the assembly code for the C while loop where i is in $s3, k is
in $s5, and the base address of the array save is in $s6
while (save[i] == k)
i += 1;
11
Logical Operations
• There are a number of bit-wise logical operations in the MIPS ISA
and $t0, $t1, $t2 #$t0 = $t1 & $t2
or $t0, $t1, $t2 #$t0 = $t1 | $t2
nor $t0, $t1, $t2 #$t0 = not($t1 | $t2)
andi $t0, $t1, 0xff00 #$t0 = $t1 & ff00
ori $t0, $t1, 0xff00 #$t0 = $t1 | ff00
12
Logic Operations
• Logic operations operate on individual bits of the operand.
$t2 = 0…0 0000 1101 0000
$t1 = 0…0 0011 1100 0000
and $t0, $t1, $t2 $t0 = 0…0 0000 1100 0000
13
Programming Styles
14
Procedures
parameters (arguments) are placed where the callee can see them
control is transferred to the callee
acquire storage resources for callee
execute the procedure
place result value where caller can access it
return control to caller
15
Jump-and-Link
• A special register (storage not part of the register file) maintains the
address of the instruction currently being executed – this is the
program counter (PC)
17
The Stack
call Proc B
Proc B’s values …
call Proc C
Proc C’s values …
… return
Stack grows return
this way return
Low address 18
Saves and Restores
19
Storage Management on a Call/Return
• A new procedure must create space for all its variables on the stack
• After the callee creates stack space, it updates the value of $sp
• Once the callee finishes, it copies the return value into $v0, frees
up stack space, and $sp is incremented
• On return, the caller/callee brings in stack values, ra, temps into registers
• The responsibility for copies between stack and registers may fall
upon either the caller or the callee
20
Example 1 (pg. 98)
22
Example 2 (pg. 101)
25
MARS Intro
26
MARS Intro
27
Example Print Routine
.data
str: .asciiz “the answer is ”
.text
li $v0, 4 # load immediate; 4 is the code for print_string
la $a0, str # the print_string syscall expects the string
# address as the argument; la is the instruction
# to load the address of the operand (str)
syscall # MARS will now invoke syscall-4
li $v0, 1 # syscall-1 corresponds to print_int
li $a0, 5 # print_int expects the integer as its argument
syscall # MARS will now invoke syscall-1
28
Example
• Write an assembly program to prompt the user for two numbers and
print the sum of the two numbers
29
Example
.data
str1: .asciiz “Enter 2 numbers:”
.text str2: .asciiz “The sum is ”
li $v0, 4
la $a0, str1
syscall
li $v0, 5
syscall
add $t0, $v0, $zero
li $v0, 5
syscall
add $t1, $v0, $zero
li $v0, 4
la $a0, str2
syscall
li $v0, 1
add $a0, $t1, $t0
30
syscall
Binary Representation
35
Example
36
Signed / Unsigned
38
MIPS Instructions
39
Sign Extension
41
Addition and Subtraction
42
Overflows
• For an unsigned number, overflow happens when the last carry (1)
cannot be accommodated
• For a signed number, overflow happens when the most significant bit
is not the same as every bit to its left
when the sum of two positive numbers is a negative result
when the sum of two negative numbers is a positive result
The sum of a positive and negative number will never overflow
• MIPS allows addu and subu instructions that work with unsigned
integers and never flag an overflow – to detect the overflow, other
instructions will have to be executed
43