0% found this document useful (0 votes)
54 views43 pages

Today's Topics: Procedures More Examples MARS Intro Number Systems

In this example, the callee leaf_example saves registers $t0, $t1, and $s0 onto the stack before performing the calculation. It loads the values of g, h, i, j from registers $a0-$a3. It performs the calculation, stores the result in $s0, and loads $s0 into the return register $v0. Finally, it restores registers $s0, $t0, $t1 from the stack before returning.

Uploaded by

Ulas Guler
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views43 pages

Today's Topics: Procedures More Examples MARS Intro Number Systems

In this example, the callee leaf_example saves registers $t0, $t1, and $s0 onto the stack before performing the calculation. It loads the values of g, h, i, j from registers $a0-$a3. It performs the calculation, stores the result in $s0, and loads $s0 into the return register $v0. Finally, it restores registers $s0, $t0, $t1 from the stack before returning.

Uploaded by

Ulas Guler
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Lecture 4: Examples, MARS, Number Systems

• Today’s topics:

 Procedures
 More examples
 MARS intro
 Number systems

1
Instructions for Making Decisions

• Decision making instructions


– alter the control flow
– i.e., change the "next" instruction to be executed

• MIPS conditional branch instructions:

bne $s0, $s1, Lbl #go to Lbl if $s0 != $s1


beq $s0, $s1, Lbl #go to Lbl if $s0 = $s1

• Example: if (i==j) h = i + j;
• bne $s0, $s1, Lbl1
add $s3, $s0, $s1
Lbl1: ...
2
Another Instruction for Changing Flow

• MIPS also has an unconditional branch instruction or jump


instruction:
j Lbl #go to Lbl

• 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

• Compile the assembly code for the C while loop where i is


in $s0, j is in $s1, and k is in $s2
while (i!=k)
i=i+j;

Loop: beq $s0, $s2, Exit


add $s0, $s0, $s1
j Loop
Exit: . . .

4
More Instructions for Making Decisions

• We have beq, bne, but what about branch-if-less-than?


• New instruction:
slt $t0, $s0, $s1 # if $s0 < $s1
# then
# $t0 = 1
# else
# $t0 = 0

5
More Instructions for Making Decisions

• Since constant operands are popular in comparisons, also


have slti
• New instruction:
slti $t0, $s0, 10 # if $s0 < 10
# then
# $t0 = 1
# else
# $t0 = 0

6
Other Branch Instructions

• Can use slt, beq, bne, and the fixed value of 0 in


$zero to create all relative conditions
– less than
slt $at, $s1, $s2 #$at set to 1 if
bne $at, $zero, Lbl # $s1 < $s2
– less than or equal to ble $s1, $s2, Lbl
– greater than bgt $s1, $s2, Lbl
– great than or equal to bge $s1, $s2, Lbl

7
Case (Switch) Statement

switch (k) { Memory


case 0: h=i+j; break; /*k=0*/
case 1: h=i+h; break; /*k=1*/
case 2: h=i-j; break; /*k=2*/ L2
• Assuming three sequential words in memory starting at L1
the address in $t4 have the addresses of the labels L0, $t4 L0
L1, and L2 and k is in $s2
add $t1, $s2, $s2 #$t1 = 2*k
add $t1, $t1, $t1 #$t1 = 4*k
add $t1, $t1, $t4 #$t1 = addr of JumpT[k]
lw $t0, 0($t1) #$t0 = JumpT[k]
jr $t0 #jump based on $t0
L0: add $s3, $s0, $s1 #k=0 so h=i+j
j Exit
L1: add $s3, $s0, $s3 #k=1 so h=i+h
j Exit
L2: sub $s3, $s0, $s1 #k=2 so h=i-j
Exit: . . . 8
MIPS Instructions, so far
Category Instr OpCd Example Meaning
Arithmetic add 0 & 20 add $s1, $s2, $s3 $s1 = $s2 + $s3
(R & I subtract 0 & 22 sub $s1, $s2, $s3 $s1 = $s2 - $s3
format)
add immediate 8 addi $s1, $s2, 4 $s1 = $s2 + 4
Data load word 23 lw $s1, 100($s2) $s1 = Memory($s2+100)
transfer store word 2b sw $s1, 100($s2) Memory($s2+100) = $s1
(I format)
Cond. br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L
branch br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L
(I format)
set on less a slti $s1, $s2, if ($s2<100) $s1=1;
than immediate 100 else $s1=0
(R format) set on less 0 & 2a slt $s1, $s2, $s3 if ($s2<$s3) $s1=1;
than else $s1=0
Uncond. jump 2 j 2500 go to 10000
jump jump register 0 & 08 jr $t1 go to $t1
9
Shift Operations
• Need operations to pack and unpack 8-bit characters into 32-bit words
• Shifts move all the bits in a word left or right
sll $t2, $s0, 8 #$t2 = $s0 << 8 bits
srl $t2, $s0, 8 #$t2 = $s0 >> 8 bits

 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;

Loop: sll $t1, $s3, 2


add $t1, $t1, $s6 lw
$t0, 0($t1) bne $t0, $s5,
Exit addi $s3, $s3, 1
j Loop
Exit: . . .

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

or $t0, $t1 $t2 $t0 = 0…0 0011 1101 0000

nor $t0, $t1, $t2 $t0 = 1…1 1100 0010 1111

13
Programming Styles

14
Procedures

• Each procedure (function, subroutine) maintains a scratchpad of


register values – when another procedure is called (the callee), the
new procedure takes over the scratchpad – values may have to be
saved so we can safely return to the caller

 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)

• The procedure call is executed by invoking the jump-and-link (jal)


instruction – the current PC (actually, PC+4) is saved in the register
$ra and we jump to the procedure’s address (the PC is accordingly
set to this address)
jal NewProcedureAddress

• Since jal may over-write a relevant value in $ra, it must be saved


somewhere (in memory?) before invoking the jal instruction

• How do we return control back to the caller after completing the


callee procedure?
16
MIPS Registers

17
The Stack

The register scratchpad for a procedure seems volatile –


it seems to disappear every time we switch procedures –
a procedure’s values are therefore backed up in memory
on a stack
High address
Proc A’s values Proc A

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

• Before/after executing the jal, the caller/callee must save relevant


values in $s0-$s7, $a0-$a3, $ra, temps into the stack space

• Arguments are copied into $a0-$a3; the jal is executed

• 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)

int leaf_example (int g, int h, int i, int j) leaf_example:


{ addi $sp, $sp, -12
int f ; sw $t1, 8($sp)
f = (g + h) – (i + j); sw $t0, 4($sp)
return f; sw $s0, 0($sp)
} add $t0, $a0, $a1
add $t1, $a2, $a3
sub $s0, $t0, $t1
Notes: add $v0, $s0, $zero
In this example, the callee took care of lw $s0, 0($sp)
saving the registers it needs. lw $t0, 4($sp)
lw $t1, 8($sp)
The caller took care of saving its $ra and addi $sp, $sp, 12
$a0-$a3. jr $ra

Could have avoided using the stack altogether.


21
Saving Conventions

• Caller saved: Temp registers $t0-$t9 (the callee won’t


bother saving these, so save them if you care), $ra (it’s
about to get over-written), $a0-$a3 (so you can put in
new arguments)

• Callee saved: $s0-$s7 (these typically contain “valuable”


data)

22
Example 2 (pg. 101)

int fact (int n) fact:


{ slti $t0, $a0, 1
if (n < 1) return (1); beq $t0, $zero, L1
else return (n * fact(n-1)); addi $v0, $zero, 1
} jr $ra
L1:
addi $sp, $sp, -8
sw $ra, 4($sp)
Notes: sw $a0, 0($sp)
The caller saves $a0 and $ra addi $a0, $a0, -1
in its stack space. jal fact
Temp register $t0 is never saved. lw $a0, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
mul $v0, $a0, $v0
jr $ra
23
MARS

• MARS is a simulator that reads in an assembly program


and models its behavior on a MIPS processor

• Note that a “MIPS add instruction” will eventually be


converted to an add instruction for the host computer’s
architecture – this translation happens under the hood

• To simplify the programmer’s task, it accepts


pseudo-instructions, large constants, constants in
decimal/hex formats, labels, etc.

• The simulator allows us to inspect register/memory


values to confirm that our program is behaving correctly
24
MARS Intro

• Directives, labels, global pointers, system calls

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

• The binary number

01011000 00010101 00101110 11100111


Most significant bit Least significant bit

represents the quantity


0 x 231 + 1 x 230 + 0 x 229 + … + 1 x 20

• A 32-bit word can represent 232 numbers between


0 and 232-1
… this is known as the unsigned representation as
we’re assuming that numbers are always positive
31
Negative Numbers

32 bits can only represent 232 numbers – if we wish to also represent


negative numbers, we can represent 231 positive numbers (incl zero)
and 231 negative numbers

0000 0000 0000 0000 0000 0000 0000 0000two = 0ten


0000 0000 0000 0000 0000 0000 0000 0001two = 1ten

0111 1111 1111 1111 1111 1111 1111 1111two = 231-1

1000 0000 0000 0000 0000 0000 0000 0000two = -231


1000 0000 0000 0000 0000 0000 0000 0001two = -(231 – 1)
1000 0000 0000 0000 0000 0000 0000 0010two = -(231 – 2)

1111 1111 1111 1111 1111 1111 1111 1110two = -2
1111 1111 1111 1111 1111 1111 1111 1111two = -1 32
2’s Complement
0000 0000 0000 0000 0000 0000 0000 0000two = 0ten
0000 0000 0000 0000 0000 0000 0000 0001two = 1ten

0111 1111 1111 1111 1111 1111 1111 1111two = 231-1

1000 0000 0000 0000 0000 0000 0000 0000two = -231


1000 0000 0000 0000 0000 0000 0000 0001two = -(231 – 1)
1000 0000 0000 0000 0000 0000 0000 0010two = -(231 – 2)

1111 1111 1111 1111 1111 1111 1111 1110two = -2
1111 1111 1111 1111 1111 1111 1111 1111two = -1
Why is this representation favorable?
Consider the sum of 1 and -2 …. we get -1
Consider the sum of 2 and -1 …. we get +1

This format can directly undergo addition without any conversions!


Each number represents the quantity
x31 -231 + x30 230 + x29 229 + … + x1 21 + x0 20 33
2’s Complement
0000 0000 0000 0000 0000 0000 0000 0000two = 0ten
0000 0000 0000 0000 0000 0000 0000 0001two = 1ten

0111 1111 1111 1111 1111 1111 1111 1111two = 231-1

1000 0000 0000 0000 0000 0000 0000 0000two = -231


1000 0000 0000 0000 0000 0000 0000 0001two = -(231 – 1)
1000 0000 0000 0000 0000 0000 0000 0010two = -(231 – 2)

1111 1111 1111 1111 1111 1111 1111 1110two = -2
1111 1111 1111 1111 1111 1111 1111 1111two = -1
Note that the sum of a number x and its inverted representation x’ always
equals a string of 1s (-1).
x + x’ = -1
x’ + 1 = -x … hence, can compute the negative of a number by
-x = x’ + 1 inverting all bits and adding 1

Similarly, the sum of x and –x gives us all zeroes, with a carry of 1


In reality, x + (-x) = 2n … hence the name 2’s complement 34
Example

• Compute the 32-bit 2’s complement representations


for the following decimal numbers:
5, -5, -6

35
Example

• Compute the 32-bit 2’s complement representations


for the following decimal numbers:
5, -5, -6

5: 0000 0000 0000 0000 0000 0000 0000 0101


-5: 1111 1111 1111 1111 1111 1111 1111 1011
-6: 1111 1111 1111 1111 1111 1111 1111 1010

Given -5, verify that negating and adding 1 yields the


number 5

36
Signed / Unsigned

• The hardware recognizes two formats:

unsigned (corresponding to the C declaration unsigned int)


-- all numbers are positive, a 1 in the most significant bit
just means it is a really large number

signed (C declaration is signed int or just int)


-- numbers can be +/- , a 1 in the MSB means the number
is negative

This distinction enables us to represent twice as many


numbers when we’re sure that we don’t need negatives
37
MIPS Instructions

Consider a comparison instruction:


slt $t0, $t1, $zero
and $t1 contains the 32-bit number 1111 01…01

What gets stored in $t0?

38
MIPS Instructions

Consider a comparison instruction:


slt $t0, $t1, $zero
and $t1 contains the 32-bit number 1111 01…01

What gets stored in $t0?


The result depends on whether $t1 is a signed or unsigned
number – the compiler/programmer must track this and
accordingly use either slt or sltu

slt $t0, $t1, $zero stores 1 in $t0


sltu $t0, $t1, $zero stores 0 in $t0

39
Sign Extension

• Occasionally, 16-bit signed numbers must be converted


into 32-bit signed numbers – for example, when doing an
add with an immediate operand

• The conversion is simple: take the most significant bit and


use it to fill up the additional bits on the left – known as
sign extension

So 210 goes from 0000 0000 0000 0010 to


0000 0000 0000 0000 0000 0000 0000 0010

and -210 goes from 1111 1111 1111 1110 to


1111 1111 1111 1111 1111 1111 1111 1110 40
Alternative Representations

• The following two (intuitive) representations were discarded


because they required additional conversion steps before
arithmetic could be performed on the numbers

 sign-and-magnitude: the most significant bit represents


+/- and the remaining bits express the magnitude

 one’s complement: -x is represented by inverting all


the bits of x

Both representations above suffer from two zeroes

41
Addition and Subtraction

• Addition is similar to decimal arithmetic

• For subtraction, simply add the negative number – hence,


subtract A-B involves negating B’s bits, adding 1 and A

Source: H&P textbook

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy