+ All Categories
Home > Documents > Goal: Implement Functions in Assembly

Goal: Implement Functions in Assembly

Date post: 07-Jan-2016
Category:
Upload: hedy
View: 25 times
Download: 0 times
Share this document with a friend
Description:
Goal: Implement Functions in Assembly. When executing a procedure (function in C) the program must follow the next 6 steps: 1. Place parameters in a place the procedure can access them. 2. Jump to the procedure code. 3. Allocate storage needed for the procedure. 4. Perform the task. - PowerPoint PPT Presentation
21
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly When executing a procedure (function in C) the program must follow the next 6 steps: 1. Place parameters in a place the procedure can access them. 2. Jump to the procedure code. 3. Allocate storage needed for the procedure. 4. Perform the task. 5. Place the result(s) in a place the calling program can access. 6. Return to the point of origin. 1/17
Transcript
Page 1: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Goal: Implement Functions in AssemblyWhen executing a procedure (function in C) the

program must follow the next 6 steps:

1. Place parameters in a place the procedure can access them.

2. Jump to the procedure code.

3. Allocate storage needed for the procedure.

4. Perform the task.

5. Place the result(s) in a place the calling program can access.

6. Return to the point of origin.

1/17

Page 2: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Registers for ProceduresMIPS software allocates the following of its

32 registers for procedure calling:$a0 - $a3: 4 argument registers $v0 - $v1: 2 return value registers $ra: return address register to return to point of origin.

2 new instructions that support procedures are:jal ProcedureAddress

Jump and link, jump to the procedure and save the address of the following instruction in $ra.

jr registerJump register, jump to the address in the register.

2/17

Page 3: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Using More RegistersIf the compiler needs more than the 4 input

and 2 output registers, it can use other registers.But it must restore the values in those registers to

the values before the procedure was called.This is a case of register spill, register values are

saved in memory.The ideal structure for saving registers is the stack,

values are pushed onto the stack before the procedure call and poped out after.

A register called the stack pointer ($sp) points to the address in memory where the stack resides.

3/17

Page 4: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Compiling a Leaf Procedureint leaf(int g,int h,int i,int j){

int f;// the arguments are in $a0-$a3,

f=(g+h) - (i+j); //f is in $s0

return f;

}

Before continuing we will introduce a new instruction:

addi $t0,$t0,10# $t0=$t0+10

add immediate (addi) has as one of its operands an immediate value. addi is of the I-type instructions and in fact gives the format its name.

4/17

Page 5: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

The Assembly Versionsubi $sp,$sp,12#make room for 3 items on stack

sw $t1,8($sp) #save $t1

sw $t0,4($sp) #save $t0

sw $s0,0($sp) #save $s0

add $t0,$a0,$a1 # $t0=g+h

add $t1,$a2,$a3 # $t1=i+j

sub $s0,$t0,$t1 # f=$t0+$t1

add $v0,$s0,$zero # $v0=$s0

lw $s0,0($sp) #restore $s0

lw $t0,4($sp) #restore $t0

lw $t1,8($sp) #restore $t1

addi $sp,$sp,12#remove the room on the stack

jr $ra #jump back to point of origin

Page 6: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Picture of Stack

MIPS software offers 2 classes of registers:$t0-$t9: 10 temporary registers, not saved by the procedure. $s0-$s7: 8 saved registers, saved by the procedure.

Contents of register $s0

Contents of register $t0

Contents of register $t1

$sp

$sp

$sp

High address

Low address a. b. c.

6/17

Page 7: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Nested ProceduresProcedures that don't call others are called leaf procedures, procedures that call others are called nested procedures.

Problems may arise now, for example the main program calls procedure A with the argument 3 in $a0 and the return address in $ra. Procedure A then calls procedure B with the argument 7 in $a0 and with its return address in $ra. The previous address saved in $ra is destroyed.

We must, somehow, preserve these values across calls.

Lets look at a translation of the recursive factorial function in C++.

Page 8: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Factorial in C++int fact(int n)

{

if(n < 1)

return (1);

else

return (n * fact(n -1));

}The function calls itself multiple times.The argument n is in register $a0, which is saved

on the stack along with $ra.

7/17

Page 9: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Factorial in Assemblyfact: subi $sp,$sp,8 # make room for 2 items

sw $ra,4($sp)# push the return address

sw $a0,0($sp)# push the argument n

slt $t0,$a0,1 # test for n<1

beq $t0,$zero,L1 # if n>=1 goto L1

li $v0,1 # pseudoinstruction $v0=1

addi $sp,$sp,8 # pop 2 items off stack

jr $ra

The following is the recursive call to fact(n-1)

L1: subi $a0,$a0,1 # n--

jal fact # call fact(n-1)

lw $a0,0($sp) # return from fact(n-1)

lw $ra,4($sp) # pop n and return address

addi $sp,$sp,8 # pop 2 items off stack

mult $v0,$a0,$v0 # return n * fact(n-1)

jr $ra

Page 10: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Procedure FrameThe stack above $sp is preserved ( נשמר ) by making sure that the callee ( פונקציה נקראת ) doesn't write above $sp.

$sp is preserved by adding exactly the amount subtracted from it.

All other registers are preserved by being saved on the stack.

The stack also contains local variables that don't fit into registers.

The segment of the stack containing the saved registers and local variables is called the procedure frame or activation record.

Page 11: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Frame PointerSome MIPS software use the register $fp to

point to the first word of the procedure frame or activation record.

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

9/17

Page 12: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Static and Automatic VariablesC++ has two storage classes for variables

automatic and static.Automatic variables are local to a function and are

deleted when the function exits.Static variables exist across function calls. Global

C++ variables are static, as well as any variables defined with the keyword static. All other variables are automatic.

MIPS software reserves a register called the global pointer or $gp. Static variables are accessed through this register.

10/17

Page 13: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Immediate Operands As mentioned before the I-format instruction contains a 16 bit constant called an immediate. Using I-type instructions avoids loading values from memory into a register. Examples of instructions which use immediate values are:multi $s0,$s1,4 # $s0=$s1*4slti $t0,$s2,10 # $t0=1 if $s2<10

But if a constant is larger than 16 bits? MIPS provides an instruction lui that loads a 16 bit constant into the upper half of an register.In order to load the value:0000 0000 0011 1101 0000 1001 0000 0000 into $s0 we must perform:lui $s0,61 #61d = 0000 0000 0011 1101addi $s0,$s0,2304 # 2304d = 0000100100000000

Page 14: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Addressing in Branches and Jumpsj 10000 # go to location 10000

Jumps to the address 10000 in memory. 2 10000 6 bits (opcode) 26 bits (address)

bne $s0,$s1,Exit # branch if $s0!=$s1 5 16 17 Exit 6 bits 5 bits 5 bits 16 bits (address)

Problem: Addresses have to fit into a 16-bit field, no program could be larger than 64KByte.

Solution: Specify a register which would be added to the branch address. But which register?

11/17

Page 15: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

PC-Relative AddressingThe Program Counter (PC) is a register that

always contains the address of the current instruction being executed.

By using the PC as the register to add to the branch address we can always branch within a range of -215 to 215-1 bytes of the current instruction.

This is enough for most loops and if statements.This is called PC-relative addressing. Procedures are not usually within a short range of

the current instruction and thus jal is a J-type instruction.

12/17

Page 16: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Branch Offset in Machine Language Lets look at a loop in assembly:Loop: . . bne $t0,$t1,Exit subi $t0,$t0,1 j LoopExit:

The machine code of the bne instruction is: 5 8 9 8

The branch instruction adds 8 bytes to the PC and not 12 because the PC is automatically incremented by 4 when an instruction is executed.

In fact the branch offset is 2 not 8. All MIPS instructions are 4 bytes long thus the offset is in words not bytes. The range of a branch has been multiplied by 4.

13/17

Page 17: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Branch Offset Example48 subi$t0,$t1,1035

52 bne $zero,$t0,L1

. . .

76 L1: jr $raWhat is the machine code translation of bne?

opcode: 5, the opcode of bne.rs: 0, $zero is register #0.rt: 9, $t0 is register #9.address: (L1 - PC)/4 = (76 - 56)/4 = 20/4 = 5

And if L1 is at address 28?address: (L1 - PC)/4 = (28 - 56)/4 = -28/4 = -7

14/17

Page 18: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Pseudodirect Addressing The 26-bit field in the jump instruction is

also a word address. Thus it is a 28-bit address. But the PC holds 32-bits?

The MIPS jump instruction replaces only the lower 28 bits of the PC, leaving the 4 highest bits of the PC unchanged.

48 j L2. . .334500 L2: add …

What is the machine code translation of j?opcode: 2, the opcode of j.address: PC32-29 || 83625

15/17

Page 19: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Addressing Mode SummaryImmediate addressing - the Operand is a constantRegister addressing - the Operand is a registerBase or displacement addressing - the operand is at

the memory location whose address is the sum of a register and a constant in the instruction.

PC-relative addressing - the address is the sum of the PC and a constant in the instruction.

Pseudodirect addressing - the jump address is the 26 bits of the instruction concatenated ( מצורף ) to the upper bits of the PC.

16/17

Page 20: Goal: Implement Functions in Assembly

Immediate addressing - the Operand is a constantaddi $t0,$t1,102 # $t0=$t1 + 10andi $s0,$s0,16 # $s0=$s0 & 16

Register addressing - the Operand is a registeraddi $t0,$t1,102 andi $s0,$s0,16jr $s4 # jump to the address in $s4

Base or displacement addressing - the operand is at the memory location whose address is the sum of a register and a constant in the instruction.lw $t0,20($gp) # $t0 = $gp[5]lb $t1,7($s5) # $t0 = $s5[7]

PC-relative addressing - the address is the sum of the PC and a constant in the instruction.beg $s0,$s1,Label # if $s0<$s1 jump to Label

Pseudodirect addressing - the jump address is the 26 bits of the instruction concatenated ( מצורף ) to the upper bits of the PC.j L37jal strcmp

Page 21: Goal: Implement Functions in Assembly

Computer Structure - The Instruction Set (2)

Addressing Modes (picture)

Byte Halfword Word

Registers

Memory

Memory

Word

Memory

Word

Register

Register

1. Immediate addressing

2. Register addressing

3. Base addressing

4. PC-relative addressing

5. Pseudodirect addressing

op rs rt

op rs rt

op rs rt

op

op

rs rt

Address

Address

Address

rd . . . funct

Immediate

PC

PC

+

+

17/17


Recommended