assembly language chapter 3. 2301274chapter 3 assembly languages2 computer languages high-level...
TRANSCRIPT
Assembly Language
Chapter 3
2301274 Chapter 3 Assembly Languages 2
Computer Languages High-level languages
Java, Python, C, ASP, HTML, … Low-level languages
Assembly language: symbolic representation of machine instructions.
Assembler: a compiler which translates from an assembly language to a machine language.
Machine languages
2301274 Chapter 3 Assembly Languages 3
Components of a computer: More detail
registers ALU
Control units
Instruction register
Program counter
program
data
data
address
memoryprocessor
2301274 Chapter 3 Assembly Languages 4
Levels of descriptions of computer systems
Applications
Operating systems
Instruction set
Functional units
Finite state machine
Logic gates
Electronics
• computer architecture begins at the instruction set.
• An instruction set is what a programmer at the lowest level sees of a processor
• one instruction set with different level of performance for many models, based on the implementation of a control unit via “microprogram”.
• Present day processor designs converge, their instruction sets become more similar than different.
2301274 Chapter 3 Assembly Languages 5
RegistersName Reg. No. Usage $zero 0 hardwired 0 $v0-$v1 2-3 return value and expression evaluation $a0-$a3 4-7 arguments $t0-$t7 8-15 temporary values$s0-$s7 16-23 saved values $t8-$t9 24-25 more temporary values $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address
2301274 Chapter 3 Assembly Languages 6
MIPS operandsName Example Comments
32 registeee
$0, $1, $2,..., $31 eee eeeee ee e eeee e. , ata must be in registers to perfo rm arithmetic.
memory words
Memory[0],Memory[4],..., Memory[4293967292]
Accessed only by data transfer instructions . MIPS uses byte addresses, so sequential words differ by 4 . Memory holds data structures, such as arrays, and spilled registers, such as those saved on procedure calls
2301274 Chapter 3 Assembly Languages 7
MIPS Assembly Instructions: Arithmetic
Instruction Example Meaning Commentsadd add $rd,$rs,$rt $rd = $rs + $rt
3 operands; data in registerssubtract sub $rd,$rs,$rt $rd = $rs - $rt
add unsignedaddu $rd,$rs,$rt
$rd = $rs + $rt3 operands; data in registers
subtract unsigned
subu $rd,$rs,$rt $rd = $rs - $rt
add immediateaddi $rd,$rs, 100
$rd = $rs + 1003 operands; data in registers and a constant
2301274 Chapter 3 Assembly Languages 8
MIPS Assembly Instructions: Data Transfer
Instruction Example Meaning Comments
load word lw $rt ,10($rs)
$rt =Memory[$rs+10]
e eee eeee e ee eee ee eeeeeeee
store word sw $rt ,
10($rs)Memory[$rs+10 ] =$rt
Data from register to memory
2301274 Chapter 3 Assembly Languages 9
MIPS Assembly Instructions: Logical
Instruction Example Meaning Comments
and and $rd,$rs,$rt $rd = $rd & $rt
3 register operands;bitwise operation
or or $rd,$rs,$rt $rd = $rd | $rt
nor nor $rd,$rs,$rt$rd = ~($rd & $rt)
and immediate ande $rt,$rs,10 $rt = $rs & 10 bitwise operation with constant
or immediate eee $rt,$rs,10 $rt = $rs | 10
shift left logical sll $rd,$rt,10 $rd = $rt << 10Shift left/right by constantshift right
logicalsrl $rd,$rt,10 $rd = $rt >> 10
2301274 Chapter 3 Assembly Languages 10
MIPS Assembly InstructionsInstruction Example Meaning Comments
jump j 10000 goto 10000 eee e ee eeeeee eeee.
jump register
j $31 goto $31 For switch, procedure ret
urn
jump and link
1000jal$31 = PC+4 ; go to 1000 For procedure call
2301274 Chapter 3 Assembly Languages 11
MIPS Assembly Instructions: Conditionals Instruction Example Meaning Comments
branch on = beq $rs,$rt,10
0
if ($rs == $rt )go to PC+4+100
eeeee ee eeeeeee; e branch
branch on not =
bne $rs,$rt,100
if ($rs != $rt )go to PC+4+100
eeeee eeeee ee ee;lative branch
set on < slt $rd,$rs,$rt
if ($rs < $rt ) $rd =1; else $rd =0
Compare < e2 ’e eemplement
set < immediate
slti $rt,$rs,100if ($rs < 100 ) $rt =1; else $rt = 0
Compare < constant e2 ’ s complement
set < unsigned
sltu $rd,$rs,$rtif ($rs < $rt ) $rd =1; else $rd =0
Compare < ; naturalnumber
set < immediate unsigned
sltiu $rt,$rs,100
($rs < 100 ) $rt = 1; else $rt = 0
Compare < constant ; natural number
2301274 Chapter 3 Assembly Languages 12
Example x = y+z+5; w = z-y;
lw $t0, 18($zero) # load y
lw $t1, 22($zero) # load z
add $t2, $t0, $t1 # y+zaddi $t2, $t2, 5 # (y+z)
+5sw $t2, 14($zero) #
store xsub $t3, $t1, $t2 # z-ysw $t3, 10($zero) # store
w
if (a<b) a++;else b++;
lw $t0, 50($zero) # load alw $t1, 54($zero) # load bslt $t2, $t0, $t1 # a<bbeq $t2, $zero, else # ifaddi $t0, $t0, 1 # a++sw $t0, 50($zero) # store aelse:addi $t1, $t1, 1 # b++sw $t1, 54($zero) # store b
w x y z10 14 18 22
a b50 54
2301274 Chapter 3 Assembly Languages 13
Basic instruction formats
31 26 25 21 20 16 15 11 10 6 5 0
op rs rt rd Shamt funct
31 26 25 21 20 16 15 0
Op rs rt Const/addr
31 26 25 0
Op code address
R- format
I- format
J- format
2301274 Chapter 3 Assembly Languages 14
Instruction Representation: Examples
InstructionForma
tOp rs rt rd Shamt funct
Const/addr
add $rs,$rt,$rd R 00reg
reg
reg 00 20 NA
sub $rs,$rt,$rd R 00reg
reg
reg 00 22 NA
addi $rs,$rt, 100
I 08reg
reg
NA NA NA const
j 10000 J 02 NA NA NA NA NA addr
beq $rs,$rt, 1000
I 04reg
reg
NA NA NA const
2301274 Chapter 3 Assembly Languages 15
Instruction Representation: Examples
Instruction FormatOp
rs rt rdSham
tfunct Const/addr
add $rs,$rt,$rd R 00reg
reg
reg
00 20 NA
31 26 25 21 20 16 15 11 10 6 5 0
op rs rt rd Shamt funct
0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0
0 1 0 9 5 0 3 0
add $t0, $t1, $t2
2301274 Chapter 3 Assembly Languages 16
Instruction Representation: Examples
InstructionForma
tOp
rs rt rd Shamtfunc
tConst/addr
sub $rs,$rt,$rd
R 00reg
reg
reg
00 22 NA
31 26 25 21 20 16 15 11 10 6 5 0
op rs rt rd Shamt funct
0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0
0 2 1 1 9 0 2 2
sub $s0, $s1, $s2
2301274 Chapter 3 Assembly Languages 17
Instruction Representation: Examples
InstructionForma
tOp
rs rt rdSham
tfunct Const/addr
addi $rs,$rt, 100
I 08reg
reg
NA NA NA const
31 26 25 21 20 16 15 0
op rs rt Const/addr
0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 0
2 2 1 1 0 C A C
addi $s0, $s1, 100
2301274 Chapter 3 Assembly Languages 18
Instruction Representation: Examples
Instruction FormatOp
rs rt rdSham
tfunct Const/addr
j 10000 J 02 NA NA NA NA NA addr
31 26 25 0
op Const/addr
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 1 0 0 0 0
0 8 0 0 2 7 1 0
j 10000
2301274 Chapter 3 Assembly Languages 19
Instruction Representation: Examples
InstructionForma
tOp
rs rt rd Shamtfunc
tConst/addr
beq $rs,$rt, 1000
I 04reg
reg
NA NA NA const
31 26 25 21 20 16 15 0
op rs rt Const/addr
0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 0
1 2 1 1 0 C A C
beq $s0, $s1, 100
2301274 Chapter 3 Assembly Languages 20
MIPS Simulator
http://pages.cs.wisc.edu/~larus/spim.html
2301274 Chapter 3 Assembly Languages 21
Case/ Switch statementswitch (i){ case 0: i++; break;
case 1: j++; break;case 4: k++;}
Jump (address) table Table storing addresses of different cases
3010 2010 k=0
3014 2045 k=1
3018 2077 k=2
301C 2077 k=3
3020 2085 k=4
2301274 Chapter 3 Assembly Languages 22
Case statement and jr instruction
switch (i){ case 0:
i++;break;
case 1:j++;break;
case 4:k++;
}
# variable i is in $t1, const 5 is in $t0# start addr of jump table (e.g. 3010) is in $s0
blt $t1, $zero, outbge $t1, $t0, out
# multiply i by 4 for word sizesll $t1, $t1, 2
# find entry in the jump tableadd $t1, $t1, $s0
# load addr in jump table to $t2lw $t2, 0($t1)jr $t2
L2010:…j out
L20485:…
out:
2301274 Chapter 3 Assembly Languages 23
Procedure calls Steps to Execute procedure
place parameters in a place where procedure can access them
transfer control to the procedure acquire the storage needed for the procedure perform the desired task place the result value in a place where the calling
program can access it return to the control point of origin
2301274 Chapter 3 Assembly Languages 24
Memory allocation for programs
Stack
Heap (dynamic data)
Static data
Code(text segment)
Reserved
$sp
$fp
$gp
pc
2301274 Chapter 3 Assembly Languages 25
Registers $a0-$a3
four argument registers in which to pass parameters
$v0-$v1 two value registers in which to return values
$ra one return address register to return to the
point of origin
2301274 Chapter 3 Assembly Languages 26
jal jump and link jumps to an address and simultaneously
saves the address of the following instruction in register $ra jal ProcedureAddress jal puts PC + 4 in $ra
to return, after completion of the procedure: jr $ra
calling program (caller) puts parameter values in $a0-$a3
callee performs calculations and places result in $v0-$v1
2301274 Chapter 3 Assembly Languages 27
Call sequence Put arguments in $a0-$a3 The rest of the arguments (if exist) are
placed in the frame or activation record Allocate a frame in stack (update $sp) Save values of $s0-$s7, $fp, $ra in the
frame Update $fp Execute jal instruction
2301274 Chapter 3 Assembly Languages 28
Return sequence Put the return value in $v0 Restore registers $s0-$s7 Restore $fp Pop the frame from stack Execute jr $ra
2301274 Chapter 3 Assembly Languages 29
Example 1int leaf_example (int g, int h, int i, int j){ int f;
f = (g + h) - (i + j); return f;}
leaf example:addi $sp, $sp, -12 #adjust stack to make room for 3 itemssw $t1, 8($sp) #save register $t1 for use afterwards
sw $t0, 4($sp) #save register $t0 for use afterwardssw $s0, 0($sp) #save register $s0 for use afterwardsadd $t0, $a0, $a1 #register $t0 contains g + hadd $t1, $a2, $a3 #register $t1 contains i + jsub $s0, $t0, $t1 #f = $t0 - $t1, which is (g+h)-(i+j)add $v0, $s0, $zero #returns f ($v0 = $s0 + 0)lw $s0, 0($sp) #restore register $s0 for callerlw $t0, 4($sp) #restore register $t0 for callerlw $t1, 8($sp) #restore register $t1 for calleraddi $sp, $sp, 12 #adjust stack to delete 3 itemsjr $ra #jump back to calling routing
2301274 Chapter 3 Assembly Languages 30
Example 2int fact (int n){ if (n<2) return(1); else return(n*fact(n-1);}fact:
addi $sp, $sp, -8 #adjust stack to make room for 2 itemssw $ra, 4($sp) #save the return addresssw $a0, 0($sp) #save the argument nslti $t0, $a0, 2 #test for n<2beq $t0, $zero, L1 #if n>=2 goto L1addi $sp, $sp, 8 #adjust stack to delete 2 itemsaddi $v0, $zero, 1 #else return 1jr $ra #jump back to calling routing
L1:addi $a0, $a0, -1 #if n>=1 calculate n-1jal fact #call fact with n-1lw $a0, 0($sp) #return from jal, restore argumentlw $ra, 4($sp) #restore return addressaddi $sp, $sp, 8 #adjust stack to delete 2 itemsmul $v0, $a0, $v0 # return n* fact(n-1)jr $ra #jump back to calling routing
2301274 Chapter 3 Assembly Languages 31
$ra 03A4
Execution examplefact:
addi $sp, $sp, -8sw $ra, 4($sp)sw $a0, 0($sp) slti $t0, $a0, 2beq $t0, $zero, L1addi $sp, $sp, 8 addi $v0, $zero, 1jr $ra
L1:addi $a0, $a0, -1jal factlw $a0, 0($sp)lw $ra, 4($sp)addi $sp, $sp, 8mul $v0, $a0, $v0jr $ra
Push frame
n<2ret 1
restore
n>=2ret n*(n-1)!
stack
03A4 ($ra)2($a0)
0344 ($ra)1($a0)
Call fact (2) addi $a0, $zero, 2(03A0) jal fact
(0340)
$a0 21
0344
$v0 12
int fact (int n){ if (n<2) return(1); else return(n*fact(n-1);}
… fact(2);
2301274 Chapter 3 Assembly Languages 32
32-bit constants A register can contain an integer between
-2147483648 (-231) and 2147483647 Immediate data can be between -32768 (-
215) and 32767 addi $t0, $zero, 32767
How can we use 32-bit constant? lui (load upper immediate) instruction
2301274 Chapter 3 Assembly Languages 33
load upper immediate instruction: lui Store 16-bit immediate data in the upper
half of the registerlui $t0, 16384 # put 0100 0000 0000 0000 in the upper half of $t0
To store a 32-bit constant in a registeradd $t0, $zero, $zerolui $t0, 16384addi $t0, $t0, 5435
0100 0000 0000 0000
2301274 Chapter 3 Assembly Languages 34
Jumping to far-away instruction In beq or bne instructions, only 215
distance can be specified. To jump to further instructions
beq $t0, $t1, L1...
L1:j L2...
L2:
2301274 Chapter 3 Assembly Languages 35
Memory Addressing Modes direct
mem[address] register indirect
mem[content[reg]] mem[content[address]]
register indirect displacement mem[content[reg]+displacement] mem[content[address]+displacement]
register indirect index and displacement
2301274 Chapter 3 Assembly Languages 36
MIPS Addressing Modes Register addressing (R-format)
add $t0,$t1,$t2
Base (Displacement) addressing (I-format) lw $t1, 12($t2)
Immediate addressing (J-format) addi $t0, $t1, 34
op rs rt Immediate data
op rs rt rd … funct
op rs rt address
register
register
memory+
2301274 Chapter 3 Assembly Languages 37
MIPS Addressing Modes PC-relative addressing (I-format)
beq $t0, $t1, label
Pseudo-direct addressing j label
op address
op rs rt address
memory
PC
+
PC
: