Do-While Example In C++ do { z--; while (a == b); z = b; In - - PowerPoint PPT Presentation

do while example
SMART_READER_LITE
LIVE PREVIEW

Do-While Example In C++ do { z--; while (a == b); z = b; In - - PowerPoint PPT Presentation

Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25 Comparisons Set on less than ( slt ) compares its source


slide-1
SLIDE 1

25

Do-While Example

  • In C++
  • do {
  • z--;
  • while (a == b);
  • z = b;
  • In assembly language
  • loop: addi $s2, $s2, -1
  • beq $s0, $s1, loop
  • or $s2, $s1, $zero
slide-2
SLIDE 2

26

Comparisons

  • Set on less than (slt) compares its source registers

and sets its destination register to 1 if src1 < src2 and to 0 otherwise (branch-if-less-than)

  • Syntax: slt dest, src1, src2
  • Set on less than immediate (slti) perform the same

comparison, but its second source operation is an immediate value

  • Syntax: slti dest, src1, immed
  • Combined with beq and bne these instructions can

implement all possible relational operators

slide-3
SLIDE 3

27

Relational Branches (1)

  • Branch if less than
  • slt $t0, src1, src2
  • bne $zero, $t0, label
  • Branch if greater than or equal
  • slt $t0, src1, src2
  • beq $zero, $t0, label
slide-4
SLIDE 4

28

Relational Branches (2)

  • Branch if greater than
  • slt $t0, src2, src1
  • bne $zero, $t0, label
  • Branch if less than or equal
  • slt $t0, src2, src1
  • beq $zero, $t0, label
slide-5
SLIDE 5

29

Case/Switch Statement*

  • Can be implemented like a chain of if-then-

else statements

  • Using a jump address table is faster
  • Must be able to jump to an address loaded

from memory

  • Jump register (jr) gives us that ability
  • Syntax: jr src

Instruction:

jr $t1 #go to address in $t1

slide-6
SLIDE 6

30

Compiling a Case (Switch) Statement

switch (k) { case 0: h=i+j; break; /*k=0*/ case 1: h=i+h; break; /*k=1*/ case 2: h=i-j; break; /*k=2*/

  • Assuming three sequential words in

memory starting at the address in $t4 have the addresses of the labels 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: . . .

$t4→ L2 L1 L0

Memory

slide-7
SLIDE 7

31

MIPS Organization

Processor Memory

32 bits

230 words read/write addr read data write data word address (binary) 0…0000 0…0100 0…1000 0…1100 1…1100

Register File src1 addr src2 addr dst addr write data 32 bits src1 data src2 data 32 registers ($zero - $ra) 32 32 32 32 32 32 5 5 5 PC ALU 32 32 32 32 32 1 2 3 7 6 5 4 byte address (big Endian) Fetch PC = PC+4 Decode Exec Add 32 32 4 Add 32 32 br offset

slide-8
SLIDE 8

32

Variable Index into Array

  • Previous method for accessing array

elements will only work if index is constant

  • If index is a variable we must compute the

address in code

  • Formula: address + (index × width)
  • Since width is usually a power of two, we

can use a left shift to perform the multiplication

slide-9
SLIDE 9

33

Structures

  • Like an array, a structure (struct) is made up
  • f several smaller data elements stored

contiguously in memory

  • However the elements do not have to all be

the same type

  • The compiler will construct a table mapping

each structure member to an offset

slide-10
SLIDE 10

34

Bit Fields

  • Structure members can be bit fields
  • In this case multiple members are packed

into a single byte or word

  • To retrieve the value of a bit field, AND the

word with a mask and then shift right

  • To set the value of a bit field use a left shift

and an OR

slide-11
SLIDE 11

35

Procedures*

  • A function (or procedure) is a sequence of

instructions that can be used to perform a task

  • When a function is called control transfers to the

function; once the function completes its task it returns

  • Functions may accept parameters (arguments)

and/or return a value

  • A function can also define local variables for its

use which exist only until the function returns

  • Functions can call other functions
slide-12
SLIDE 12

36

Call and Return

  • Call and return are nothing more than

unconditional jumps

  • The calling function (the caller) jumps to the

beginning of the function

  • The called function (the callee) jumps back to the

point from which it was called (return address)

  • Since a function can be called from any number of

places the return address changes with each call

slide-13
SLIDE 13

37

Jump and Link

  • The jump and link (jal) instruction performs

an unconditional jump just like j, but first saves the address of the next instruction in the return address register ($ra)

  • jal label
  • A function returns by jumping to the

address stored in that register:

  • jr $ra
slide-14
SLIDE 14

38

Example (1)

  • Calling a function:
  • jal function1
  • Function:
  • function1: add $t0, $t1, $t2
  • sub $t3, $t4, $t5
  • jr $ra
slide-15
SLIDE 15

39

Calling Conventions

  • We need a way to pass parameters to a

function and get a return value from it

  • A calling convention is a set of rules that

define how parameters and return values are passed to/from functions as well as which registers a function can use

  • Allows us to call functions compiled by

different compilers and even languages

slide-16
SLIDE 16

40

Parameters/Return Value

  • Most MIPS software uses the same calling

convention

  • The first four parameters are placed in

registers ($a0-$a3) additional parameters are placed on the stack

  • Return values go in registers $v0-$v1
slide-17
SLIDE 17

41

MIPS Registers

No

reserved Assembler Temp

1 $at Yes Global Pointer 28 $gp Yes Stack Pointer 29 $sp Yes Frame Pointer 30 $fp No Reserved for OS Kernel 26-27 $k0-$k1 Yes Return Address (hardware) 31 $ra Yes Saved Values 16-23 $s0-$s7 No Temporaries 8-15, 24-25 $t0-$t9 No Arguments 4-7 $a0-$a3 No Return Value/Temporaries 2-3 $v0-$v1 N/A Zero (hardware) $zero Saved on Call? Usage Number Name

slide-18
SLIDE 18

42

Example (2)

  • Function call
  • or $a0, $s0, $zero
  • jal function2
  • or $s0, $v0, $zero
  • Function definition
  • function2: add $v0, $a0, $a0
  • jr $ra
slide-19
SLIDE 19

43

Register Use

  • Callee-saved registers are owned by caller; if a

function uses a callee-saved register it must save the old value and restore that value before it returns

  • Caller-saved registers are owned by the callee; if a

caller is using a caller-saved register, it must save the old value before calling the function and restore it after the function returns

  • In MIPS, $s0-$s7 are callee-saved and $t0-$t9 are

caller-saved

slide-20
SLIDE 20

44

The Stack

  • Data that a function needs to save in memory is

pushed onto the stack

  • Before returning, the function pops the data off of

the stack

  • In MIPS, the stack pointer ($sp) contains the

address of the last word pushed onto the stack

  • The stack grows downward from higher addresses

to lower ones

slide-21
SLIDE 21

45

Spilling Registers

  • What if the callee needs more registers? What if the

procedure is recursive?

  • uses a stack – a last-in-first-out queue – in memory for

passing additional values or saving (recursive) return address(es)

 One of the general registers, $sp, is

used to address the stack (which “grows” from high address to low address)

 add data onto the stack – push

$sp = $sp – 4 data on stack at new $sp

 remove data from the stack – pop

data from stack at $sp $sp = $sp + 4

low addr high addr $sp top of stack

slide-22
SLIDE 22

46

Accessing the Stack

  • Push a word onto the stack
  • addi $sp, $sp, -4
  • sw src, 0($sp)
  • Pop a word off of the stack
  • lw dest, 0($sp)
  • addi $sp, $sp, 4
  • Typically we batch multiple pushes and

pops together

slide-23
SLIDE 23

47

Accessing the Stack (cont'd)

  • Push three words onto the stack
  • addi $sp, $sp, -12
  • sw $t0, 8($sp)
  • sw $t1, 4($sp)
  • sw $s0, 0($sp)
  • Pop three words off of the stack
  • lw $s0, 0($sp)
  • lw $t1, 4($sp)
  • lw $t0, 8($sp)
  • addi $sp, $sp, 12
slide-24
SLIDE 24

48

Stack Illustration

Contents of register $s0 Contents of register $t0 Contents of register $t1 $sp $sp $sp

High address Low address a. b. c.

slide-25
SLIDE 25

49

Data on the Stack

  • Data a function puts on the stack makes up

its stack frame or activation record

  • If there are more than four parameters, the

extra parameters must be pushed onto the stack

  • Non-leaf functions must save the value of

the return address register ($ra)

slide-26
SLIDE 26

50

Data on the Stack (cont'd)

  • Any registers that must be saved are stored
  • n the stack
  • Also local variables that won't fit into

registers (either because there are no registers left or the data is too big)

  • Commonly a frame pointer ($fp) is used in

addition to the stack pointer, since the stack pointer could change during the function

slide-27
SLIDE 27

51

Stack Frame

Saved argument registers (if any) Local arrays and structures (if any) Saved saved registers (if any) Saved return address b. $sp $sp $sp c. $fp $fp $fp a.

High address Low address

slide-28
SLIDE 28

52

Where Data is Stored

  • The static data area holds global variables
  • Fixed size
  • In MIPS, the global pointer ($gp) contains the address
  • f this area
  • The stack holds local variables and function-

related information

  • Dynamic and managed automatically by the compiler
  • The heap holds data allocated through new or

malloc

  • Dynamic and managed by the programmer
slide-29
SLIDE 29

53

Program Memory

$sp $gp 0040 0000

hex

1000 0000

hex

Text Static data Dynamic data Stack 7fff ffff

hex

1000 8000

hex

pc Reserved

slide-30
SLIDE 30

54

MIPS Registers

No Assembler Temporary 1 $at Yes Global Pointer 28 $gp Yes Stack Pointer 29 $sp Yes Frame Pointer 30 $fp No Reserved for OS Kernel 26-27 $k0-$k1 Yes Return Address 31 $ra Yes Saved 16-23 $s0-$s7 No Temporaries 8-15, 24-25 $t0-$t9 No Arguments 4-7 $a0-$a3 No Return Value/Temporaries 2-3 $v0-$v1 N/A Zero $zero Saved on Call? Usage Number Name