Assembly 1
Assembly 1
Introduction
Assembly Language:
Human-Readable Machine Language
ADD R6,R2,R6
§ How big of a pain was it to write/read machine instructions
§Assembler is a program that turns symbols into
machine instructions.
1
Programming in Assembly
§ Assembly language level is one-step up from machine
• All instructions used in Assembly are actual machine
instructions….somewhat!
• Use mnemonics and address labels to make it easier to understand
the program
o Labels converted to addresses and offsets by assembler
• “macros” and utilities to make it easier
§ Assembler directives
• Tell assembler what to do without the programmer explicitly writing out
the machine code to do the task
• Allocating storage
• Initializing data
optional mandatory
4
2
Opcodes and Operands
§Opcodes
• reserved symbols that correspond to actual (LC-3) instructions
• listed in Appendix A
o ex: ADD, AND, LD, LDR, …
§Operands
• registers -- specified by Rn, where n is the register number
• numbers -- indicated by # (decimal) or x (hex)
• label -- symbolic name of memory location
• separated by comma
• number, order, and type correspond to instruction format
o ex:
ADD R1,R1,R3
ADD R1,R1,#3
LD R6,NUMBER
BRz LOOP
§Comment
• anything after a semicolon is a comment
• ignored by assembler
• used by humans to document/understand programs
• tips for useful comments:
o avoid restating the obvious, as “decrement R1”
o provide additional insight, as in “accumulate product in R6”
o use comments to separate pieces of program
3
Assembler Directives
§Pseudo-operations.. To make programmer’s life easier
•do not refer to operations executed by program
• used by assembler
• look like instruction, but “opcode” starts with dot
Trap Codes
§LC-3 assembler provides “pseudo-instructions” for
each trap code, so you don’t have to remember
them… more on TRAP instructions later…
Code Equivalent Description
HALT TRAP x25 Halt execution and print message to
console.
IN TRAP x23 Print prompt on console,
read (and echo) one character from keybd.
Character stored in R0[7:0].
OUT TRAP x21 Write one character (in R0[7:0]) to console.
GETC TRAP x20 Read one character from keyboard.
Character stored in R0[7:0].
PUTS TRAP x22 Write null-terminated string to console.
Address of string is in R0.
4
; Example Assembly Program – Add 2 to non-negative
number and store into another memory location
; load number from locations PLACE1,
.ORIG x3000 ;program starts at address x3000
LD R1, PLACE1 ; PLACE is location in memory
; note: offset not specified by
programmer
; assembler calculates offset needed
BRn Done ;if number is Negative goto end
ADD R3, R1, #2 ; Add 2 store into R3
ST R3, PLACE2 ; store result into PLACE2
Done HALT ;halt program
;
PLACE2 .BLKW 1 ; reserve/set aside one word in memory
PLACE1 .FILL x0005 ; initialize number to 5
Label
.END ; end of program
10
10
5
; Example Assembly Program – Add 2 to non-negative
number and store into another memory location
; load number from locations PLACE1,
.ORIG x3000 ;program starts at address x3000
LD R1, PLACE1 ; PLACE is location in memory
; note: offset not specified by programmer
BRn Done ;if number is Negative goto end
ADD R3, R1, #2 ; Add 2 store into R3
ST R3, PLACE2 ; store result into PLACE2
Done HALT ;halt program
;
PLACE2 .BLKW 1 Decimal #
PLACE1 .FILL x0005 Binary b
Hex x
.END ; end of program
11
12
6
Assembly Process
§Assembler: Converts assembly language file (.asm)
into an executable file (.obj) …for the LC-3 simulator in our case.
§First Pass:
• scan program file
• find all labels and calculate the corresponding addresses;
this is called the symbol table
§Second Pass:
• convert instructions to machine language,
using information from symbol table
13
13
14
14
7
Pass 1
§Construct the symbol table for the program
Symbol Address
Done
PLACE2
PLACE1
15
15
§Potential problems:
• Improper number or type of arguments
o ex: NOT R1,#7
ADD R1,R2
ADD R3,R3,NUMBER
16
16
8
Pass 2
§Using the symbol table constructed earlier,
translate these statements into LC-3 machine language.
BRp AGAIN
LD R2, NUMBER
17
17
18
18
9
Assembly Process
§Assembler: Converts assembly language file (.asm)
into an executable file (.obj) …for the LC-3 simulator in our case.
§First Pass:
• scan program file
• find all labels and calculate the corresponding addresses;
this is called the symbol table
§Second Pass:
• convert instructions to machine language,
using information from symbol table
19
19
20
20
10
Pass 1
§Construct the symbol table for the program
Symbol Address
Done
PLACE2
PLACE1
21
21
§Potential problems:
• Improper number or type of arguments
o ex: NOT R1,#7
ADD R1,R2
ADD R3,R3,NUMBER
22
22
11
Pass 2
§Using the symbol table constructed earlier,
translate these statements into LC-3 machine language.
BRp AGAIN
LD R2, NUMBER
23
23
LC-3 Assembler
§Using “assemble” (Unix) or LC3Edit (Windows),
generates several different output files.
This one gets
loaded into the
simulator.
24
24
12
Multiple Object Files
§An object file is not necessarily a complete program.
• system-provided library routines
• code blocks written by multiple developers
25
25
26
26
13
Style Guidelines
1. Provide a program header…standard stuff
2. Start labels, opcode, operands, and comments in same
column for each line. (Unless entire line is a comment.)
3. Use comments to explain what each register does.
4. Give explanatory comment for most instructions.
5. Use meaningful symbolic names.
1. Mixed upper and lower case for readability.
2. ASCIItoBinary, InputRoutine, SaveR1
6. Provide comments between program sections.
27
27
§ Flowcharts anyone ?
§ Decomposition:
• Break problem/solution into sub-problems/modules
o Structured programming
• Connect the modules…
o With conditionals, iterations, sequence,….
28
28
14
Example
§ Array of N numbers
§ Read length N of the array
§ Replace negative numbers by 0
§ Add all the (new) numbers
§ Print the sum
29
29
Task
True
Subtask 1 Subtask 2
Subtask 2 Subtask
30
15
Sequential
§do Subtask 1, then subtask 2, etc.
Read value of N
from keyboard
Print Sum)
31
31
Conditional
§If condition is true, do Subtask 1;
else, do Subtask 2.
Yes No
x<0
Check if number >=0
Change –ve to 0
x=0 x=x
Next subtask…
(add values)
32
32
16
Iterative
§Do Subtask over and over,
as long as the test condition is true.
True
33
33
§Sequential
• Instructions naturally flow from one to the next,
so no special instruction needed to go
from one sequential subtask to the next.
34
17
Code for Conditional
PC offset to
Exact bits depend address C
on condition Instruction
being tested A
Generate
True False Condition
Test
Condition B 0000 ? C
Subtask 1
Subtask 1 Subtask 2
0000 111 D
C
Subtask 2
Unconditional branch
Next to Next Subtask D
Subtask Next PC offset to
Subtask address D
Assuming all addresses are close enough that PC-relative branch can be used.
35
35
36
18
Converting Code to Assembly
§ Can use a standard template approach
§ Typical Constructs
• if/else
• while
• do/while
• for
37
37
if/else
if(x > 0) LD R1, X
{ BRP THEN
r2 = r3 + r4; ADD R5,R6,R7
} BRNZP DONE
else THEN ADD R2,R3,R4
{ DONE ...
r5 = r6 + r7;
}
38
19
if/else
if(x > 0) LD R1,X
{ BRNZ ELSE
r2 = r3 + r4; ADD R2,R3,R4
} BRNZP DONE
else ELSE ADD R5,R6,R7
{ DONE ...
r5 = r6 + r7;
}
39
while
x = 0; AND R1,R1,#0
i = 10; AND R2,R2,#0
while(i > 0) ADD R1,R1,#10
{ WHL BRNZ DONE
x = x + i; ADD R2,R2,R1
i--; ADD R1,R1,#-1
} BRNZP WHL
40
20