Title: Assembly
1Assembly
2Outline
- Assembly Language Programming
- Example Assembly Language Program
- Instructions, Pseudo-ops (assembler directives),
Example - The Assembly Process
- Intro, Two-pass process, First pass (Symbol
Table), Second pass (Generating Machine Language - Beyond Assembly of a Single Program
- Executable Image, gt1 Object File
3Assembly Language
- Low level language
- Dependent on ISA
- Contrast with high level languages
- FORTRAN, C, etc.
- Typically converted to assembly!
- User friendly
- mnemonics not numbers
- Doing more than just the instructions
4- Multiply a number by 6
- .ORIG x3050
- LD R1, SIX R1 is loop counter
- LD R2, NUMBER
- AND R3, R3, 0 Clr R3, will hold product
- The Loop
- AGAIN ADD R3, R3, R2 Summing into R3
- ADD R1, R1, -1 Dec loop counter
- BRp AGAIN
- HALT
-
- NUMBER .BLKW 1
- SIX .FILL x0006
- .END
5- Multiply a number by 6
- .ORIG x3050
- LD R1, GEORGE R1 is loop counter
- LD R2, NUMBER
- AND R3, R3, 0 Clr R3, will hold product
- The Loop
- AGAIN ADD R3, R3, R2 Summing into R3
- ADD R1, R1, -1 Dec loop counter
- BRp AGAIN
- HALT
-
- NUMBER .BLKW 1
- GEORGE .FILL x0006
- .END
6- Multiply a number by 6
- .ORIG x3050
- LD R1, SIX R1 is loop counter
- LD R2, NUMBER
- AND R3, R3, 0 Clr R3, will hold product
- The Loop
- AGAIN ADD R3, R3, R2 Summing into R3
- ADD R1, R1, -1 Dec loop counter
- BRp AGAIN
- HALT
-
- NUMBER .BLKW 1
- SIX .FILL x0006
- .END
Must have Opcode and Operands
7- Multiply a number by 6
- .ORIG x3050
- LD R1, SIX R1 is loop counter
- LD R2, NUMBER
- AND R3, R3, 0 Clr R3, will hold product
- The Loop
- AGAIN ADD R3, R3, R2 Summing into R3
- ADD R1, R1, -1 Dec loop counter
- BRp AGAIN
- HALT
-
- NUMBER .BLKW 1
- SIX .FILL x0006
- .END
Label and Comments optional
8- Multiply a number by 6
- .ORIG x3050
- LD R1, SIX R1 is loop counter
- LD R2, NUMBER
- AND R3, R3, 0 Clr R3, will hold product
- The Loop
- AGAIN ADD R3, R3, R2 Summing into R3
- ADD R1, R1, -1
- BRp AGAIN
- HALT
-
- NUMBER .BLKW 1
- SIX .FILL x0006
- .END
Decimal Binary b Hex
x
9- Multiply a number by 6
- .ORIG x3050
- LD R1, SIX R1 is loop counter
- LD R2, NUMBER
- AND R3, R3, 0 Clr R3, will hold product
- The Loop
- AGAIN ADD R3, R3, R2 Summing into R3
- ADD R1, R1, -1
- BRp AGAIN
- HALT
-
- NUMBER .BLKW 1
- SIX .FILL x0006
- .END
More than just code, need to worry about memory
allocation
10- Multiply a number by 6
- .ORIG x3050
- LD R1, SIX R1 is loop counter
- LD R2, NUMBER
- AND R3, R3, 0 Clr R3, will hold product
- The Loop
- AGAIN ADD R3, R3, R2 Summing into R3
- ADD R1, R1, -1
- BRp AGAIN
- HALT
-
- NUMBER .BLKW 1
- SIX .FILL x0006
- .END
Questions?
11Pseudo-ops (Assembler Directives)
- .ORIG
- Where to put program
- .FILL
- Initialize one location
- .BLKW n
- Set aside n words in memory
- .STRINGZ "sample"
- Initialize 7 locations (sample 0 word)
- .END
- End of assembly program
12- .orig x3000
- and r2, r2, 0 Initialize counter to 0
- ld r3, ptr Pointer to buffer
- trap x23 Get search char from user
- ldr r1, r3, 0 Get first character
- test add r4, r1, -4 Test for EOT
- brz output
- not r1, r1 Get ready for test
- add r1, r1, r0 If match R1 xFFFF
- not r1, r1 If match R1 now x0000
- brnp getchar If no match, don't inc
- add r2, r2, 1 Count matching character
- getchar add r3, r3, 1 Increment pointer
- ldr r1, r3, 0 Get next character
- brnzp test
- output ld r0, ascii Convert digit to ascii by
- add r0, r0, r2 adding template
- trap x21 Output ascii code for cnt
- trap x25 Halt program
13Assembly Process
- How does it work?
- .ORIG x3000
- ADD R3, R1 R2
14Assembly Process
- How does it work?
- .ORIG x3000
- ADD R3, R1 R2
- BRz OUTPUT
- OUTPUT
15- .orig x3000
- and r2, r2, 0 Initialize counter to 0
- ld r3, ptr Pointer to buffer
- trap x23 Get search char from user
- ldr r1, r3, 0 Get first character
-
- test add r4, r1, -4 Test for EOT
- brz output
- not r1,r1 Get ready for test
- add r1, r1, r0 If match R1 xFFFF
- not r1, r1 If match R1 now x0000
- brnp getchar If no match, don't inc
- add r2, r2, 1 Count matching character
- getchar add r3, r3, 1 Increment pointer
- ldr r1, r3, 0 Get next character
- brnzp test
- output ld r0, ascii Convert digit to ascii by
- add r0, r0, r2 adding template
- trap x21 Output ascii code for cnt
16Trap Service Routines
- The assembler will recognize aliases for the
various predefined trap instructions - TRAP x20 GETC
- TRAP x21 OUT
- TRAP x22 PUTS
- TRAP x23 IN
- TRAP x24 PUTSP
- TRAP x25 HALT
17- .orig x3000
- and r2, r2, 0 Initialize counter to 0
- ld r3, ptr Pointer to buffer
- getc Get search char from user
- ldr r1, r3, 0 Get first character
-
- test add r4, r1, -4 Test for EOT
- brz output
- not r1,r1 Get ready for test
- add r1, r1, r0 If match R1 xFFFF
- not r1, r1 If match R1 now x0000
- brnp getchar If no match, don't inc
- add r2, r2, 1 Count matching character
- getchar add r3, r3, 1 Increment pointer
- ldr r1, r3, 0 Get next character
- brnzp test
- output ld r0, ascii Convert digit to ascii by
- add r0, r0, r2 adding template
- trap x21 Output ascii code for cnt
18Executable Image
- User written code
- Other code
- Linked together
19Single Object File
.orig x3000 ... ... ptr .fill x4000 Address
of data .end .orig x 4000 ...
20Multiple Object Files
.orig x3000 ... ... ptr .fill x4000 Address
of data .end
???
21Multiple Object Files
.orig x3000 ... ... ptr .fill STARTofDATA
Address of data .end
22Multiple Object Files
.external STARTofDATA .orig x3000 ... ... ptr
.fill STARTofDATA Address of data .end
23Multiple Object Files
Note This doesn't exist!
.external STARTofDATA .orig x3000 ... ... ptr
.fill STARTofDATA Address of data .end
24Logical Disconnect?
- x3000 x2007
- x3001
- x3002
- x3003
- x3004
- x3005
- x3006
- x3007
- x3008 x002A
- .orig x3000
- LD R0, DATA
- DATA .FILL 42
?
25Labels
- Labels are used to to symbolically indicate the
address of a particular instruction or data item - The value of each label should be known at the
completion of the assembly first pass - Examples
- BRNZP POINT
- LD R1, ZAP
26Why so many Load Instructions?
- LD
- Rx ? MEMPC1Offset
- Gets the contents of a memory location into a
register - Memory location must be physically close to the
instruction - LD R1, DATA
- DATA .FILL 42
27Why so many Load Instructions?
- LDI
- Rx ? MEMMEMPC 1 Offset
- Gets the contents of the memory location pointed
to by another memory location - Typical usage device register operations
- LDI R0, KBSR
- KBSR .FILL xFE00
28Why so many Load Instructions?
- LDR
- Rx ? MEMRbase Offset
- Used to load register from an address contained
in another register ( offset) - LDR R1, R2, 3
29Why so many Load Instructions?
- LEA
- R ? PC 1 Offset
- Used to get an address into a register
- Use before LDR or STR
- Use with PUTS
- LEA R0, MSG
- PUTS
- MSG .STRINGZ "Hello, World!"
30Thought Question
- Do we need all of these load instructions?
31Need LD?
- LEA R2, DATA
- LDR R1, R2, 0
-
32Need LDI?
- LDI R1, KBSR
- KBSR .FILL xFE00
-
- LEA R2, KBSR
- LDR R1, R2, 0
- LDR R1, R1, 0
- KBSR .FILL xFE00
33Converting Code to Assembly
- Can use a standard template approach
- Typical Constructs
- if/else
- while
- do/while
- for
34if/else
- if(x gt 0)
-
- r2 r3 r4
-
- else
-
- r5 r6 r7
- LD R1, X
- BRP THEN
- ADD R5,R6,R7
- BRNZP DONE
- THEN ADD R2,R3,R4
- DONE ...
35if/else
- if(x gt 0)
-
- r2 r3 r4
-
- else
-
- r5 r6 r7
- LD R1, X
- BRNZ ELSE
- ADD R2,R3,R4
- BRNZP DONE
- ELSE ADD R5,R6,R7
- DONE ...
36while
- x 0
- i 10
- while(i gt 0)
-
- x x i
- i--
- AND R2,R2,0
- AND R1,R1,0
- ADD R1,R1,10
- WHL BRNZ DONE
- ADD R2,R2,R1
- ADD R1,R1,-1
- BRNZP WHL
37do/while
38for
39Questions?
40(No Transcript)