Class 12 The Extended Assembler.pptx

Download Report

Transcript Class 12 The Extended Assembler.pptx

The Extended Assembler
•
•
•
•
•
•
•
Pseudoinstructions
Names and assignments usage
Load immediate values
Load unknown addresses, nop
Assembler Temporary Register - $at
Load word, Store word pseudoinstructions
Arrays
Central Connecticut State University, MIPS Tutorial.
Chapters 19.
The extended assembler level
• Each instruction for the basic
assembler corresponds directly•
to one machine instruction.
• The basic assembler creates a •
view of the processor one level
above raw machine code.
The extended assembler creates
a view that is at an higher level.
The extended assembler
statements correspond to one
or several machine instructions.
Ext.Assembler - > Machine code
 SPIM can act as either a basic assembler or an
extended assembler.
 To run the program in SPIM in the settings menu put a
check for "allow pseudo instructions" and remove
the check from "bare machine".
Richer set of
instructions
Basic set of
instructions
Quite possible you’ll see the change
of Data segment initial address
0x1000 0000  0x1001 0000
Basic Assembly is a subset
Pseudo Instructions
Extended Assembly
Pseudo
Instructions
Basic Assembly
 PseudoInstruction - A common variation of assembly
language instructions often treated as if it were an
instruction in its own right.
 Pseudoinstructions give MIPS a richer set of assembly
language instructions than those implemented by the
hardware.
 The only cost is reserving one register, $at- $1, for use by
the assembler.
Pseudoinstruction examples
Instruction name change and assignment
addu $5,$0,$7
# assembly instruction
move $t5,$t7
# pseudoinstruction
 This instruction copies the contents of register $t7
into register $t5.
move d, s
• Copy the contents of the source register s to the destination
register d
• This works like assignment statement
d = s; $t5 = $t7
[0x00400000] 0x000f6821
[0x00400004] 0x000f6821
addu $13,$0,$15 ;10: addu $t5,$zero,$t7
addu $13,$0,$15 ;11: move $t5, $t7
Load immediate
li
d,value
Load register $d with the positive or negative integer "value".
Value may be any integer up to 32 bits.
 The extended assembler automatically translates the
pseudoinstructions into the most efficient sequence of
actual instructions.
li
li
li
$t0,43
$t1,-96
$t7,-16772555
#
#
#
first value
second value
third value
addu
addu
$t0,$t0,$t1
$t0,$t0,$t7
#
#
add the values
leave result in $t0
li
$t2,0x12345678
# load large numbers
li
li
$t3, 2345678901
$t4, -2000000000
Load immediate compiled examples
ori $8, $0, 43
; 7:
li
$t0,43
#first
lui $1, -1
ori $9, $1, -96
; 8:
li
$t1,-96
#second value
lui $1, -256
ori $15, $1, 4661
; 9:
li
$t7,-16772555 #third
addu $8, $8, $9
addu $8, $8, $15
; 11: addu
; 12: addu
lui $1, 4660
; 14: li
ori $10, $1, 22136
lui $1, 32767
ori $11, $1, -1
; 16: li
lui $1, -30518
; 17: li
ori $12, $1, 27648
$t0,$t0,$t1
$t0,$t0,$t7
value
value
#add the values
$t2,0x12345678 #load large number
$t3, 2345678901
$t4, -2000000000
Registers with addresses
 The past chapters have put addresses in base registers.
 That was done with code similar to the following
# Load the address of val2 into the base $10 register
lui
$10,0x1000
# top half of address
ori
$10,$10,0x0008 # bottom half of address
lw
$11,0($10)
# Load contents of memory
 We used predefined addresses
. . .
 It is
not always possible to know the address in advance
.data
 Conveniently,
addresses and systems software
val0:
.word symbolic
0
this unnecessary.
val1:make.word
1
val2:
.word
2 # base register points at "2"
Load Address Pseudoinstruction
 la d, exp
 load register $d with the address described by the expression "exp“ .
 "exp" is a symbolic address (label).
la
$10,val2
# load a 32-bit address into $10
lw
$11,0($10) # load contents of memory
. . .
.data
val0:
.word
0
val1:
.word
1
val2:
.word
2
# base register $10 points here
la example, nop
Add 2 variables taken from the memory
la
$t0,val2
# load address
lw
$t1,0($t0)
# load first value, 2
lw
$t2,4($t0)
# load second value, 3
nop
addu $t1,$t1,$t2 # calc. sum
.data
val0:
.word
0
After compiling val2 is
val1:
.word
1
transformed to address in $8
val2:
.word[val2]
2
lui
$1, 4097
; 8: la
$t0,val2
val3:
.word
3
ori
$8, $1,
8 [val2]
sll $0,$0,0
lw $9, 0($8)
lw $10, 4($8)
sll $0,$0,0
addu $9, $9, $10
;
;
;
;
; 11: nop
9: lw
$t1,0($t0)
10: lw
$t2,4($t0)
11: nop
12: addu $t1,$t1,$t2
Assembler Temporary Register $at-$1
 IfRecall
we use
it, then meaning
it could be
Registers
damaged
by–extended
 $0 – zero
is always full of zeros.
assembler.
 $1 - $at - The assembler temporary register is
 Register
usage
rule – instructions
reserved
for convention
use in the machine
dothat
not use
$at in your program
pseudoinstructions
are translated into.
written on extended assembly.
 Where is $1 in source program ?
 Can we use it in our program ?
lui $1, 4097 [val2]
ori $8, $1, 8 [val2]
lw $9, 0($8)
lw $10, 4($8)
sll $0,$0,0
; 8: la
$t0,val2
; 9: lw
; 10: lw
; 11: nop
$t1,0($t0)
$t2,4($t0)
Load Word pseudoinstruction
We are familiar with this Load Word
lw
$t1,8($t0)
#
load the word at address $t0+8
This one is more comfortable to use:
lw
d, exp
• Load register $d with the value at address exp.
• exp can be any of several expression types that evaluate
to an address (label)
Load Word example
lw $9, 4($8)
; 16: lw
$t1,4($t0)
lui $1, 4097 [val1]
lw $9, 4($1) [val1]
; 17: lw
$t1,val1
lui $1, 4097 [val1] ; 18: lw
lw $9, 12($1) [val1]
val0:
val1:
val2:
val3:
val4:
val5:
.data
.word
.word
.word
.word
.word
.word
0
1
2
3
4
5
$t1,val1+8
Store Word pseudoinstruction
lw
$t3,x
# get x
lw
$t0,a
# get a
sw
exp
lw d,$t1,bb
# get bb
# get
c at address exp.
lw
Store$t2,c
register $d into the
word
mult $t3,$t3
# x2
mflo
exp can
expression
$t4be any of several
# $t4
= x2 types that
mult
$t4,$t0
low = ax2
evaluate
to an address #(label).
mflo $t4
# $t4 = ax2
mult $t1,$t3
# low = bx
mflo $t5
# $t5 = bx
addu $t5,$t4,$t5
# $t5 = ax2 + bx
addu $t5,$t5,$t2
# $t5 = ax2 + bx + c
sw
$t5,value
# value = polynomial
.data
x:
.word
4
value: .word
1
a:
.word 20
bb:
.word -2 # the SPIM assembler does not allow the label "b“
c:
.word
5
Arrays
How to address the arrays ?
$6 for array2
 Variable to keep the index to address the array
la
$6,array2
 The initial address of array
 Index incrementing value
add $6,$6,?
l?
$16,0($6)
 How to access the array
s?
 The length of array
size
array1:
array2:
result:
.word
.byte
.half
.word
lw
$16,0($6)
$10,size
7
-30, -23, 56, -43, 72, -18, 71
45, 23, 21, -23, -82, 0, 69
0,
0, 0,
0,
0, 0, 0
Arrays
How to address the arrays ?
$6 for array2
 Variable to keep the index to address the array
la
$6,array2
 The initial address of array
 Index incrementing value
add $6,$6,2
lh
$16,0($6)
 How to access the array
sh
 The length of array
size
array1:
array2:
result:
.word
.byte
.half
.word
lw
$16,0($6)
$10,size
7
-30, -23, 56, -43, 72, -18, 71
45, 23, 21, -23, -82, 0, 69
0,
0, 0,
0,
0, 0, 0
Counting Loop with arrays
la
la
la
init:
test:
ori
lw
nop
sltu
beq
nop
$8,$0,0
$10,size
$9,$8,$10
$9,$0,endLp
endLp:
.data
size
array1:
array2:
result:
# count = 0
l?
l?
addu
. . .
s?
addiu
j
nop
nop
.word
.byte
.half
.word
$8,$8,1
test
$5,array1
$6,array2
$7,result
$15,0($5)
$16,0($6)
$17,$15,$16
$17,?($?)
# count++ ;
add
add
add
$5,$5,? #indexes
$6,$6,?
$7,$7,?
7
-30, -23, 56, -43, 72, -18, 71
45, 23, 21, -23, -82, 0, 69
0,
0, 0,
0,
0, 0, 0
Counting Loop with arrays
la
la
la
init:
test:
ori
lw
nop
sltu
beq
nop
$8,$0,0
$10,size
$9,$8,$10
$9,$0,endLp
endLp:
.data
size
array1:
array2:
result:
# count = 0
lb
lh
addu
. . .
sw
addiu
j
nop
nop
.word
.byte
.half
.word
$8,$8,1
test
$5,array1
$6,array2
$7,result
$15,0($5)
$16,0($6)
$17,$15,$16
$17,0($7)
# count++ ;
addi $5,$5,1 #indexes
addi $6,$6,2
addi $7,$7,4
7
-30, -23, 56, -43, 72, -18, 71
45, 23, 21, -23, -82, 0, 69
0,
0, 0,
0,
0, 0, 0
Store word with label and index
This is a powerful instruction to work with the arrays.
sw
$t1,Array($t0)
lui $1, 4097 [Array]
addu $1, $1, $8
sw $9, 24($1) [Array]
val0:
val1:
val2:
val3:
val4:
val5:
Array:
.data
.word
.word
.word
.word
.word
.word
.word
0
1
2
3
4
5
1,2,3,4,5,6
index
Offset or
Initial address
of array
Load word with the index and label
 This is powerful instruction to work with the arrays.
lw $t1,val1+16($t0)
index
lui $1, 4097 [val1+16 (0x00000010)]
addu $1, $1, $8
lw
$9, 20($1) [val1+16(0x00000010)($1)]
Offset or
Initial address
of array