elec3720 chapter 9 mips instructions part 2
TRANSCRIPT
Click to edit Master title style
Click to edit Master subtitle style
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 1
ELEC3720 Programmable Logic DesignELEC3720 Programmable Logic DesignChapter 9 More MIPSChapter 9 More MIPS
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 2
Instruction Set Architecture• Instruction Set Architecture (ISA)
– Instructions available in language– Machine code formatting rules
» Register conventions» Necessary resources
• Common architectures:– IA-32 (IA-32e or x86-64)– IA-64 (Itanium)– Power6 (formerly PowerPC)– ARM– MIPS– SPARC
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 3
• simple instructions all 32 bits wide• very structured• only three instruction formats
Overview of MIPS
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 4
MIPS Register Convention Saved registers must be preserved on a procedure call if used, the called
procedure (callee) saves and restore them Temporary registers are not preserved by the callee on a procedure call Reg 1 ($at) is reserved for the assembler Reg 26 ($k0) and Reg 27 ($k1) are reserved for the operating system
Name Register number Usage$zero 0 the constant value 0$v0-$v1 2-3 values for results and expression evaluation$a0-$a3 4-7 arguments$t0-$t7 8-15 temporaries$s0-$s7 16-23 saved$t8-$t9 24-25 more temporaries$gp 28 global pointer$sp 29 stack pointer$fp 30 frame pointer$ra 31 return address
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 5
For Loop (discussed in Chapter 9)• Example
int sum = 0;
for (I=0; I!=10; I=I+1){sum = sum+1;
}
add $s1,$0,$0 # sum=0addi $s0,$0,0 # i=0addi $t0,$0,10 # $t0=10
for:beq $s0,$t0,done # if i=10,
# branch to doneaddi $s1,$s1,1 # sum=sum+1addi $s0,$s0,1 # increment ij for
done:
C code MIPS code
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 6
For Loop (converting to Machine Code)
0x00 add $s1,$0,$00x04 addi $s0,$0,00x08 addi $t0,$0,10
for:0x0C beq $s0,$t0,done0x10 addi $s1,$s1,10x14 addi $s0,$s0,10x18 j fordone:
0x1C …
Registers: $s0=0x10, $s1=0x11, $t0=0x08
Instructions: add (op/funct=0x0/0x20)addi (op=0x08)j (op=0x02)
MIPS code Constructing Machine Code
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 7
Procedure CallsProcedure calling conventions:• Caller:
– passes arguments to callee.• Callee:
– must not overwrite registers or memory needed by the caller
– returns to the point of call– returns the result to caller
MIPS conventions:• Call procedure: jump and link (jal) • Return from procedure: jump register (jr)• Argument values: $a0 - $a3• Return value: $v0
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 8
Procedure Calls Use to structure programs, both to make them easier to
understand and to allow code to be reused Registers used for procedure calling
- $a0 - $a3: 4 argument registers to pass parameters- $v0 - $v1: 2 value registers to return values- $ra: 1 return address register to return to point of origin Jump-and-link instruction
- jump to an address and simultaneously save the address of the following instruction (PC+4) in register $ra
jal ProcedureAddress Jump register instruction - to do the return jump
jr $ra
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 9
Procedure Calls
High-level codeint main() {
simple();a = b + c;
}
void simple() {return;
}
MIPS assembly code
0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2...
0x00401020 simple: jr $ra
void means that simple doesn’t return a value.
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 10
Procedure Calls
High-level codeint main() {
simple();a = b + c;
}
void simple() {return;
}
MIPS assembly code
0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2...
0x00401020 simple: jr $ra
jal: jumps to simple and saves PC+4 in the return address register ($ra). In this case, $ra = 0x00400204 after jal executes.
jr $ra: jumps to address in $ra, in this case 0x00400204.
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 11
Input Arguments and Return ValuesMIPS conventions:
• Argument values: $a0 - $a3
• Return value: $v0
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 12
Input Arguments and Return ValuesHigh-level codeint main() {
int y;...y = diffofsums(2, 3, 4, 5); // 4 arguments...
}
int diffofsums(int f, int g, int h, int i){
int result;result = (f + g) - (h + i);return result; // return value
}
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 13
Input Arguments and Return ValuesMIPS assembly code# $s0 = y
main: ...addi $a0, $0, 2 # argument 0 = 2addi $a1, $0, 3 # argument 1 = 3addi $a2, $0, 4 # argument 2 = 4addi $a3, $0, 5 # argument 3 = 5jal diffofsums # call procedureadd $s0, $v0, $0 # y = returned value...
# $s0 = resultdiffofsums:add $t0, $a0, $a1 # $t0 = f + gadd $t1, $a2, $a3 # $t1 = h + isub $s0, $t0, $t1 # result = (f + g) - (h + i)add $v0, $s0, $0 # put return value in $v0jr $ra # return to caller
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 14
Input Arguments and Return ValuesMIPS assembly code# $s0 = resultdiffofsums:
add $t0, $a0, $a1 # $t0 = f + gadd $t1, $a2, $a3 # $t1 = h + isub $s0, $t0, $t1 # result = (f + g) - (h + i)add $v0, $s0, $0 # put return value in $v0jr $ra # return to caller
• diffofsums overwrote 3 registers: $t0, $t1, and $s0•diffofsums can use the stack to temporarily store registers
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 15
The Stack• Memory used to temporarily save variables• Like a stack of dishes, last-in-first-out (LIFO) queue• Expands: uses more memory when more space is needed• Contracts: uses less memory when the space is no longer
needed• PUSH stack (to save 3 registers)
addi $sp, $sp, -12sw $s0, 8($sp) sw $s1, 4($sp) sw $s2, 0($sp)
• POP stack (to restore the 3 register values)lw $s2, 0($sp) lw $s1, 4($sp) lw $s0, 8($sp) addi $sp, $sp, 12
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 16
The Stack
Data
7FFFFFFC 12345678
7FFFFFF8
7FFFFFF4
7FFFFFF0
Address
$sp 7FFFFFFC
7FFFFFF8
7FFFFFF4
7FFFFFF0
Address Data
12345678
$sp
AABBCCDD
11223344
• Grows down (from higher to lower memory addresses)• Stack pointer: $sp, points to top of the stack
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 17
How Procedures use the Stack
# MIPS assembly
# $s0 = resultdiffofsums:
add $t0, $a0, $a1 # $t0 = f + gadd $t1, $a2, $a3 # $t1 = h + isub $s0, $t0, $t1 # result = (f + g) - (h + i)add $v0, $s0, $0 # put return value in $v0jr $ra # return to caller
• Called procedures must have no other unintended side effects.
• But diffofsums overwrites 3 registers: $t0, $t1, $s0
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 18
Storing Register Values on the Stack# $s0 = resultdiffofsums:addi $sp, $sp, -12 # make space on stack
# to store 3 registers
sw $s0, 8($sp) # save $s0 on stack
sw $t0, 4($sp) # save $t0 on stack
sw $t1, 0($sp) # save $t1 on stack
add $t0, $a0, $a1 # $t0 = f + gadd $t1, $a2, $a3 # $t1 = h + isub $s0, $t0, $t1 # result = (f + g) - (h + i)add $v0, $s0, $0 # put return value in $v0lw $t1, 0($sp) # restore $t1 from stack
lw $t0, 4($sp) # restore $t0 from stack
lw $s0, 8($sp) # restore $s0 from stack
addi $sp, $sp, 12 # deallocate stack space
jr $ra # return to caller
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 19
The Stack during diffofsums Call
Data
FC
F8
F4
F0
Address
$sp
(a)
Data
FC
F8
F4
F0
Address
$sp
(b)
$s0
Data
$sp
(c)
$t0
FC
F8
F4
F0
Address
? ??
stac
k fra
me
$t1
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 20
Registers
stack below $spstack above $sp
$v0 - $v1$sp
$a0 - $a3$ra
$t0 - $t9$s0 - $s7
NonpreservedCaller-Saved
PreservedCallee-Saved
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 21
How do we run an application?
Assembly Code
High Level Code
Compiler
Object File
Assembler
Executable
Linker
Memory
Loader
Object FilesLibrary Files
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 22
What needs to be stored in memory?• Instructions (also called text)• Data
– Global/static: allocated before program begins– Dynamic: allocated within program
• How big is memory?– At most 232 = 4 gigabytes (4 GB)– From address 0x00000000 to 0xFFFFFFFF
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 23
The MIPS Memory MapSegmentAddress
0xFFFFFFFC
0x800000000x7FFFFFFC
0x100100000x1000FFFC
0x100000000x0FFFFFFC
0x004000000x003FFFFC
0x00000000
Reserved
Stack
Heap
Static Data
Text
Reserved
Dynamic Data
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 24
Running a Program
Assembly Code
High Level Code
Compiler
Object File
Assembler
Executable
Linker
Memory
Loader
Object FilesLibrary Files
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 25
Example Program: C Codeint f, g, y; // global variables
int main(void) {
f = 2;g = 3;y = sum(f, g);
return y;}
int sum(int a, int b) {return (a + b);
}
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 26
Example Program: Assembly Code
int f, g, y; // global
int main(void) {
f = 2;
g = 3;y = sum(f, g);return y;
}
int sum(int a, int b) {return (a + b);
}
.dataf:g:y:.textmain:
addi $sp, $sp, -4 # stack framesw $ra, 0($sp) # store $raaddi $a0, $0, 2 # $a0 = 2sw $a0, f # f = 2addi $a1, $0, 3 # $a1 = 3sw $a1, g # g = 3jal sum # call sumsw $v0, y # y = sum()lw $ra, 0($sp) # restore $raaddi $sp, $sp, 4 # restore $spjr $ra # return to OS
sum:add $v0, $a0, $a1 # $v0 = a + bjr $ra # return
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 27
The MIPS Memory MapSegmentAddress
0xFFFFFFFC
0x800000000x7FFFFFFC
0x100100000x1000FFFC
0x100000000x0FFFFFFC
0x004000000x003FFFFC
0x00000000
Reserved
Stack
Heap
Static Data
Text
Reserved
Dynamic Data
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 28
Example Program: Symbol Table
0x0040002Csum
0x00400000main
0x10000008y
0x10000004g
0x10000000f
AddressSymbol
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 29
Example Program: Executable
Executable file header Text Size Data Size
Text segment
Data segment
Address Instruction
Address Data
0x004000000x004000040x004000080x0040000C0x004000100x004000140x004000180x0040001C0x004000200x004000240x004000280x0040002C0x00400030
addi $sp, $sp, -4sw $ra, 0 ($sp)addi $a0, $0, 2sw $a0, 0x8000 ($gp)addi $a1, $0, 3sw $a1, 0x8004 ($gp)jal 0x0040002Csw $v0, 0x8008 ($gp)lw $ra, 0 ($sp)addi $sp, $sp, -4jr $raadd $v0, $a0, $a1jr $ra
0x100000000x100000040x10000008
fgy
0xC (12 bytes)0x34 (52 bytes)
0x23BDFFFC0xAFBF00000x200400020xAF8480000x200500030xAF8580040x0C10000B0xAF8280080x8FBF00000x23BD00040x03E000080x008510200x03E0008
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 30
Example Program: In Memory
ygf
0x03E000080x008510200x03E000080x23BD00040x8FBF00000xAF8280080x0C10000B0xAF8580040x200500030xAF8480000x200400020xAFBF00000x23BDFFFC
MemoryAddress
$sp = 0x7FFFFFFC0x7FFFFFFC
0x10010000
0x00400000
Stack
Heap
$gp = 0x10008000
PC = 0x00400000
0x10000000
Reserved
Reserved
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 31
Exceptions• Unscheduled procedure call to the exception handler• Caused by:
– Hardware, also called an interrupt, e.g. keyboard– Software, also called traps, e.g. undefined instruction
• When exception occurs, the processor:– Records the cause of the exception– Jumps to the exception handler at instruction address
0x80000180– Returns to program
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 32
Exception Registers• Not part of the register file.
– Cause
» Records the cause of the exception– EPC (Exception PC)
» Records the PC where the exception occurred• EPC and Cause: part of Coprocessor 0• Move from Coprocessor 0
– mfc0 $t0, EPC
– Moves the contents of EPC into $t0
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 33
Exception Causes
0x00000030Arithmetic Overflow
0x00000028Undefined Instruction
0x00000024Breakpoint / Divide by 0
0x00000020System Call
0x00000000Hardware InterruptCauseException
ELEC3720-2009-B6-PSB ACADEMY-LKH-Chapter 9 34
Exceptions• Processor saves cause and exception PC in Cause
and EPC• Processor jumps to exception handler (0x80000180)• Exception handler:
– Saves registers on stack– Reads the Cause register
mfc0 Cause, $t0
– Handles the exception– Restores registers– Returns to program
mfc0 EPC, $k0
jr $k0