mehmet can vuran, instructor university of nebraska-lincoln
DESCRIPTION
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln. CSCE 230, Fall 2013 Chapter 2 (part 1) Introduction to Assembly Language ( §2.1–2.5 and §B .1–B.6). Acknowledgement: Overheads adapted from those provided by the authors of the textbook. - PowerPoint PPT PresentationTRANSCRIPT
CSCE 230, Fall 2013
Chapter 2 (part 1)Introduction to Assembly Language(§2.1–2.5 and §B.1–B.6)
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln
Acknowledgement: Overheads adapted from those provided by the authors of the textbook
Simplified Assembly Notation Used in Chapter 2
Assembly languages for different processors often use different mnemonics for a given operation.
To avoid the need for details of a particular assembly language at this early stage, Chapter 2 uses English words rather than processor specific mnemonics.
This would, hopefully, ease the learning of specific assembly languages described in Appendices A–D: Nios II (App. B) – RISC (used in 230L) Coldfire (App. C) – CISC ARM (App. D) – RISC Intel IA-32 (App. E) – CISC
2
3
Mapping of Variables
4
HLL to Assembly Language: Example 1
Translate D = A+B+C to assembly Translation Process:
1. In assembly, can only add two at a time, hence
D = A+B;D = D+C
2. Assign variables:For safe keeping in memory: Locations named #A, #B, #C, and #DTemporarily, to processor registers R1–R4 respectively.(Can think of each variable as having a static image in memory and dynamic image in registers.)
3. Code each assignment statement in part 1) to assembly.
Translation 1
Load R1, #ALoad R2, #BAdd R4, R1, R2 # D = A+BLoad R3, #CAdd R4, R4, R3 # D = D+CStore R4, #D
5
6
HLL to Assembly Language: Example 1 Revisited
Translate HLL Statement: D = A+B+C to assembly, minimizing the use of registers
Key Idea: Not all variables are needed concurrently in the registers for doing the computation. Can reuse registers to minimize their number.
Translation 2Load R1, #ALoad R2, #BAdd R2, R1, R2Load R1, #CAdd R2, R1, R2Store R2, #D
7
• Note that both R1 and R2 are reused to represent different variables during the computation.• A good compiler tries to minimize register usage.
Assembly Language Basics
8
9
Instruction Characteristics Number of operands: One, two, or
three Type: Arithmetic, Logical, Data
Transfer, Control Transfer Instruction Length: Fixed or variable
– we’ll consider fixed Addressing Modes: How operands
are found
Special Register R0
In many RISC architectures (including Nios II), register R0 is defined to be a read-only, constant register with value 0.
R0 = 0 Can use to implement new
instructions with existing ones, e.g. Add R1, R0, R2 == Move R1,
R210
Pseudoinstructions In the previous examples, Move is not really
implemented on the processor but the assembler can translate it into a real instruction.
In general, a pseudoinstruction can be thought of as a macro translated by the assembler into one or more ISA instructions.
One of the ways, the assembly language can be made to appear richer than the ISA of the processor.
11
Addressing Modes
12
Load R1, #A
#A ?
Addressing Modes
13
Addressing Modes: Indirection and Pointers
Register, absolute, and immediate modesdirectly provide the operand or address
Other modes provide information from which the effective address of operand is derived
For program that steps through an array, canuse register as pointer to next number and use the Indirect mode to access array elements:
Load R2, (R5)14
15
Addressing Modes: Indexing
Consider index mode in: Load R2, X(R5) Effective address is given by [R5] X For example, assume operand address is
1020, 5 words (20 bytes) from start of array at 1000
Can put start address in R5 and use X20 Alternatively, put offset in R5 and use X1000 Base with index mode: Load Rk, X(Ri, Rj) Effective address is given by [Ri] [Rj] X
16
17
R5
18
R5
For Loop Semantics Example: for i=0, i<N, i=i+1 {Body of Loop}
Semantics of the three parts: The first part, i=0, done once, before the loop is
entered The second part, the condition, i<N, is evaluated. If true, the body of the loop is executed. Then the
third part, the re-initialization step i=i+1 is done and the condition is reevaluated.
The loop terminates when the condition becomes false.
19
For Loop Implementation
20
Intermediate Code
Register Map
i NR1 R2
Move R1, #0Loop: Branch_if_(i>=N) Exit
{Body of Loop}Add R1, R1, #1Branch Loop
Exit: …
Assembly Code
for i=0, i<N, i=i+1 {Body of
Loop}
i=0;Loop: if(!(i<N)) goto Exit
{Body of Loop}i = i+1goto Loop
Exit: …
For Practice
Try implementing the following program control constructs: If (condition) then {…} else {…} While (condition) {Body of Loop} Do {Body of Loop} until (condition)
21
Stepping through an Array: Solution 1 – Explicit Indexing Example
for i=0, i<N {A[i] = A[i]+1}
Maintain the base A[0] and offset of A[i] from A[0] in two separate registers.
Register Map:
22
i N A[i] #A=Address A[0]
Offset A[i]
R1 R2 R3 R4 R5
Stepping through an Array: Solution 1 –Index Arithmetic (Contd.)
Initialization: R5 = 0; R4 = Address of (A[0]) Code: Move R5, #0; Load R4, #A
Body of Loop: Code:
Load R3, (R4,R5) # R3 = A[i]Add R3, R3, #1Store R3, (R4,R5) # A[i] = A[i]+1Add R5, R5, 4 # Update offset for the next
23
i N A[i] Address A[0]
Offset A[i]
R1 R2 R3 R4 R5
Stepping through an Array: Solution 2: Pointer Arithmetic
Maintain pointer to A[i] in a register
Initialization: R4 = Address of A[0]Load R4, #A
Body of Loop:Load R3, (R4) # R3 = A[i]Add R3, R3, #1Store R3, (R4) # A[i] = A[i]+1Add R4, R4, 4 # Update pointer to array element
24
25
Array Processing: A Larger Example
Find the max of N numbers: A[0], A[1], …, A[N-1].
HLL Code:Max = A[0]for i=1, i<N {
if(A[i]>Max) Max = A[i]}
HLL to Assembly Code
26
Intermediate Code
Max = A[0]i = 1
Loop: if(!(A[i])>Max)) goto SkipMax = A[i]
Skip: i = i+1if(N>i) goto Loop
Max = A[0]for i=1, i<N {
if(A[i]>Max) Max = A[i]}
Move R3, #NLoad R3, (R3)Move R5, #ALoad R1, (R5)Move R2, #1
Loop: Add R5, R5, #4Load R4, (R5)Branch_if_(R1>=R4) SkipMove R1, R4
Skip: Add R2, R2, #1Branch_if_(R3>R2) LoopMove R2, #MaxStore R1, (R2)
Assembly Code
Variable:
Max
i #N =(Addr.
N)
A[i]
#A=Addr. A[0]
Register
R1 R2 R3 R4 R5
Note: R2 is reused after the for loopto store the address #Max
Reading Assignment
For another example of array processing using a for loop: Read and study the LISTADD example,
Section 2.4.3 (pp. 45–47) of the textbook.
27
Loading Large Constants
In the last example, “Load R5, #A” cannot be a RISC ISA instruction if #A is an absolute 32-bit address. Why?
Because it is a pseudoinstruction that must be expanded to real instruction.
General problem, solved in RISC processors by assembling 32-bit constants in two parts: high and low, e.g.
Load_high R5, #A31-16
Add R5, R5, # A15-0
28
Real Assembly Languages (§2.5)
Mnemonics (LD/ADD instead of Load/Add) used when programming specific computers
The mnemonics represent the OP codes Assembly language is the set of mnemonics
and rules for using them to write programs The rules constitute the language syntax Example: suffix ‘I’ to specify immediate mode
ADDI R2, R3, 5 (instead of #5)
29
Assembler Directives
Other information also needed to translate source program to object program
How should symbolic names be interpreted? Where should instructions/data be placed? Assembler directives provide this information ORIGIN defines instruction/data start position RESERVE and DATAWORD define data storage EQU associates a name with a constant value
30
31
32
Assembler DirectiveAddress Label
Pseudo-opOperatio
nOperand
s
Register Operan
ds
Memory Address
esAssembled
Machine Code
Immediate
Operand
Comment
Nios IIAssembly
Lang.
Nios II Assembly Language(§B.1–B.6)
35
Registers Register names
should always in lower-case in Nios II assembler
Other symbols, e.g. labels, are case-sensitive
r0 is constant 0 Don’t use r1 in your
programs. r2–r23 can be freely
used as general-purpose regs.
36
Addressing Modes
37
Load/Store
Load Form
ldw r2, 20(r3)/* Load word instruction */ Can also apply to bytes and halfword
(ldb, ldh) Can control sign extension for byte and
halfword operand by using ldb (sign extended) or ldbu (sign not extended)
Store: Similarly38
Pseudoinstructions Move
movri, rj movi ri, value-16 /* 16-bit value
*/ movui ri, value-16 /* unsigned */ movia ri, LABEL /* 32-bit value
– typically an address */ Assembler implements as:
orhi ri, r0, LABEL_HIGHori ri,ri, LABEL_LOW
39
Branch and Jump
Form br LABEL beq ri, rj, LABEL
where LABEL is 16-bit offset relative to PC Signed and Unsigned versions, e.g,
beq and bequ Full range of comparisons:
beq, bne, bge, bgt, ble, plus their unsigned versions 40
Pseudoinstructions
Move type already mentioned Subtract-Immediate:
subi ri, rj, value-16 == addi ri, rj, -value-16
Branch Greater Than Signedbgt ri, rj, LABEL == blt rj, ri, LABEL
41
Assembler Directives
.org Value /* ORIGIN */ .equLABEL, Value /* LABEL = Value
*/ .byte expression /* Places byte
size data into memory */
.halfword and .word work similarly .skip size /* Reserves memory
space */ .end/* End of source-code file */ 42
Example Assembly Program: Ch. 2 Notation vs. Nios II
43
movia r3, N /* addr. N */ldw r3, (r3)movia r5, Aldw r6, (r5)movi r2, 1
Loop: addi r5, r5, 4ldw r4, (r5)bge r6, r4, Skipmov r6, r4
Skip: addi r2, r2, 1bgt r3, r2, Loopmovia r2, Maxstw r6, (r2)
Chapter 2 Nios II
Note: R1 is mapped to r6 because r1 is reserved as an assembler temporary register in Nios II.
Move R3, #NLoad R3, (R3)Move R5, #ALoad R1, (R5)Move R2, #1
Loop: Add R5, R5, #4Load R4, (R5)Branch_if_(R1>=R4) SkipMove R1, R4
Skip: Add R2, R2, #1Branch_if_(R3>R2) LoopMove R2, #MaxStore R1, (R2)
Adding Data Segment
44
movia r3, Nldw r3, (r3)movia r5, Aldw r6, (r5)movi r2, 1
Loop: addi r5, r5, 4ldw r4, (r5)bge r6, r4, Skipmov r6, r4
Skip: addi r2, r2, 1bgt r3, r2, Loopmovia r2, Maxstw r6, (r2).org 2000 /* Optional */
Max: .skip 4 /* Reserve 4 bytes for Max */N: .word 20 /* Reserve word for N, initialized to 20 */A: .skip 80 /* Reserve 80 bytes, or 20 words, for array A */
Nios II
Program Assembly & Execution
From source program, assembler generates machine-language object program
Assembler uses ORIGIN and other directivesto determine address locations for code/data
For branches, assembler computes ±offsetfrom present address (in PC) to branch target
Loader places object program in memory Debugger can be used to trace execution
45
Program Assembly Example
Consider two-pass assembly relative to starting address of 0: Pass 1 builds the
symbol table Pass 2 generates
code
46
movia r3, Nldw r3, (r3)movia r5, Aldw r6, (r5)movi r2, 1
Loop: addi r5, r5, 4ldw r4, (r5)bge r6, r4, Skipmov r6, r4
Skip: addi r2, r2, 1bgt r3, r2, Loopmovia r2, Maxstw r6, (r2).org 2000
Max: .skip 4N: .word 20A: .skip 80
Pass 1
47
0 movia r3, Nldw r3, (r3)movia r5, Aldw r6, (r5)movi r2, 1
Loop: addi r5, r5, 4ldw r4, (r5)bge r6, r4, Skipmov r6, r4
Skip: addi r2, r2, 1bgt r3, r2, Loopmovia r2, Maxstw r6, (r2).org 2000
Max: .skip 4N: .word 20A: .skip 80
Symbol ValueN undefined
Symbol Table
Pass 1
48
0 movia r3, N4 ldw r3, (r3)8 movia r5, A
ldw r6, (r5)movi r2, 1
Loop: addi r5, r5, 4ldw r4, (r5)bge r6, r4, Skipmov r6, r4
Skip: addi r2, r2, 1bgt r3, r2, Loopmovia r2, Maxstw r6, (r2).org 2000
Max: .skip 4N: .word 20A: .skip 80
Symbol ValueN undefinedA undefined
Symbol Table
Pass 1
49
0 movia r3, N4 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120 Loop: addi r5, r5, 4
ldw r4, (r5)bge r6, r4, Skipmov r6, r4
Skip: addi r2, r2, 1bgt r3, r2, Loopmovia r2, Maxstw r6, (r2).org 2000
Max: .skip 4N: .word 20A: .skip 80
Symbol ValueN undefinedA undefined
Loop 20
Symbol Table
Pass 1
50
0 movia r3, N4 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip
mov r6, r4Skip: addi r2, r2, 1
bgt r3, r2, Loopmovia r2, Maxstw r6, (r2).org 2000
Max: .skip 4N: .word 20A: .skip 80
Symbol ValueN undefinedA undefined
Loop 20Skip undefined
Symbol Table
Pass 1
51
0 movia r3, N4 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip32 mov r6, r436 Skip: addi r2, r2, 1
bgt r3, r2, Loopmovia r2, Maxstw r6, (r2).org 2000
Max: .skip 4N: .word 20A: .skip 80
Symbol ValueN undefinedA undefined
Loop 20Skip undefined ->
36
Symbol Table
Pass 1
52
0 movia r3, N4 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip32 mov r6, r436: addi r2, r2, 140 bgt r3, r2, Loop20
movia r2, Maxstw r6, (r2).org 2000
Max: .skip 4N: .word 20A: .skip 80
Symbol ValueN undefinedA undefined
Loop 20Skip 36
Symbol Table
Pass 1
53
0 movia r3, N4 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip32 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, Max
stw r6, (r2).org 2000
Max: .skip 4N: .word 20A: .skip 80
Symbol ValueN undefinedA undefined
Loop 20Skip 36Max undefined
Symbol Table
Pass 1
54
0 movia r3, N4 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip32 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, Max
stw r6, (r2).org 2000
2000 Max: .skip 4N: .word 20A: .skip 80
Symbol ValueN undefinedA undefined
Loop 20Skip 36Max undefined -
>2000
Symbol Table
Pass 1
55
0 movia r3, N4 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120 : addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip32 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, Max
stw r6, (r2).org 2000
2000: .skip 42004: N .word 20A: .skip 80
Symbol ValueN undefined -
>2004A undefined
Loop 20Skip 36Max 2000
Symbol Table
Pass 1
56
0 movia r3, N4 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip32 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, Max
stw r6, (r2).org 2000
2000: .skip 42004: .word 202008 A: .skip 802088 …
Symbol ValueN 2004A undefined-
>2008Loop 20Skip 36Max 2000
Symbol Table
Pass 1
57
0 movia r3, N4 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip32 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, Max
stw r6, (r2).org 2000
2000: .skip 42004: .word 202008: .skip 802088 …
Symbol ValueN 2004A 2008
Loop 20Skip 36Max 2000
Symbol Table
Pass 2
58
0 movia r3, N 20044 ldw r3, (r3)8 movia r5, A12 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip32 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, Max
stw r6, (r2).org 2000
2000: .skip 42004: .word 202008: .skip 802088 …
Symbol ValueN 2004A 2008
Loop 20Skip 36Max 2000
Symbol Table
Pass 2
59
0 movia r3, 20044 ldw r3, (r3)8 movia r5, A200812 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip32 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, Max
stw r6, (r2).org 2000
2000: .skip 42004: .word 202008: .skip 802088 …
Symbol ValueN 2004A 2008
Loop 20Skip 36Max 2000
Symbol Table
Pass 2
60
0 movia r3, 20044 ldw r3, (r3)8 movia r5, 200812 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, Skip3632 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, Max
stw r6, (r2).org 2000
2000: .skip 42004: .word 202008 A: .skip 802088 …
Symbol ValueN 2004A 2008
Loop 20Skip 36Max 2000
Symbol Table
Pass 2
61
0 movia r3, 20044 ldw r3, (r3)8 movia r5, 200812 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, 3632 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, Max2000
stw r6, (r2).org 2000
2000: .skip 42004: .word 202008 A: .skip 802088 …
Symbol ValueN 2004A 2008
Loop 20Skip 36Max 2000
Symbol Table
Pass 2
62
0 movia r3, 20044 ldw r3, (r3)8 movia r5, 200812 ldw r6, (r5)16 movi r2, 120: addi r5, r5, 424 ldw r4, (r5)28 bge r6, r4, 3632 mov r6, r436: addi r2, r2, 140 bgt r3, r2, 2044 movia r2, 2000
stw r6, (r2).org 2000
2000: .skip 42004: .word 202008 A: .skip 802088 …
Symbol ValueN 2004A 2008
Loop 20Skip 36Max 2000
Symbol Table