Professional Documents
Culture Documents
Assembly Language / Development Process Assembly Language / Development Process
Assembly Language / Development Process Assembly Language / Development Process
Problem: must convert ideas (human thoughts) into an assembler: program – converts programs from
executing program (binary image in memory) assembly language to object format
Need: DEVELOPMENT PROCESS • object format: an intermediate format
people-friendly way to write programs – mostly binary, but may include other info
tools to support conversion to binary image linker: program that combines object files to
create an “executable” file
assembly language: used by people to describe
programs loader: loads executable files into memory, and
• syntax: set of symbols + grammar rules for may initialize some registers (e.g. IP )
constructing statements using symbols
• semantics: what is meant by statements; ultimately: Assembler, linker and loader are tools.
the binary image
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 1 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 2
Development Process
Editor
human readable p86 Assembly Language
people results (including
work .LST assembly errors) people create .ASM files using assembly
here .ASM
language
syntax must account for all aspects of a
may link multiple
Assembler .OBJ
.OBJ
OBJ files
program and development process:
• constant values
Linker .EXE
• reserve memory to use for variables
loader is part of
operating system • write instructions: operations & operands
Loader
(or possibly – addressing modes
debugger)
memory
• directives to tools in development process
simulator loads
.OBJ files directly processor IP
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D.Computer
Hutchinson,System
L. Marshall Sept. 2001 3 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 4
•1
p86 Assembly Language: Label
p86 Assembly Language: Label Definition
Address/Reference
label represents address of first allocated byte after definition
used by assembler to decide exact address • e.g. DoThis: MOV AX, [ BX ]
must be first non-blank text on a line • DoThis represents address of first byte of the MOV
instruction
user-defined name followed by “:”
label reference: use of label in an operand
name must start with alpha A .. Z a .. z • refers to address assigned by assembler (no “:”)
then contain: alpha, numeric, ‘_’ control flow example (assume CX contains loop counter):
DoWhile:
• e.g. Continue: L8R: Out_2_Lunch: CMP CX, 0
cannot redefine reserved words JE DoneDoWhile
…
• e.g. MOV: is illegal JMP DoWhile
DoneDoWhile:
MOV AX, . . . (etc.)
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 7 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 8
p86 Assembly Language: Memory Declarations p86 Assembly Language: Memory Declarations
reserve memory for variables Examples (continued):
2 sizes: DW ; reserve 2 consecutive bytes
• DB reserves a byte of memory
Z: DW ; reserves 2 bytes – label Z is defined to represent the
• DW reserves a word (2 bytes) of memory ; address of the first byte
may also provide an (optional) initialization value as an
operand W: DW 256 ; reserve 2 bytes – label W etc., and initialize the bytes
Examples: ; to 256 decimal (little endian !!!)
DB ; reserves one byte
HUH: DW W ; reserve 2 bytes – label HUH etc., and initialize the
X: DB ; reserves one byte – label X is ; bytes to contain the address of the variable W above
; defined to represent the address A:
; of the byte B:
Y: DB 3 ; reserve one byte – label Y etc. DB ‘C’ ; reserves 1 byte – labels A and B both represent the
; and initialize the byte to 3 : address of the byte, and initializes it to 43H
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 9 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 10
•2
p86 Assembly Language: 2 Pass Assembly Process
p86 Assembly Language: Tool Directives
statements that are intended to help tools each pass: processes all statements in .ASM file sequentially
are not assembled directly into instructions or memory from start to finish
declarations 1st Pass: for each statement:
1. check syntax
END Directive:
2. allocate any memory needed for image
directive to 2 tools: assembler and loader – memory declaration (DB, DW)
directs assembler to stop reading from .ASM file – instruction: opcode + operands
• any subsequent statements are ignored 3. if includes a label definition: assign value to label and keep record of
operand: must be a label reference (label, value) association in Symbol Table
• interpreted as specifying the address of the first if syntax errors in 1st pass, then write errors to .LST file and
instruction to be executed stop, else ....
• directs loader to load specified address into IP after 2nd Pass: build binary for each statement:
loading .OBJ file • may require calculating offsets – may result in errors – e.g. trying to
jump too far for a conditional jump (target out of range)
Syntax: • write results to .LST file
END label-reference • if no errors – write results to .OBJ file
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 13 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 14
p86 Assembly Language: 1st Pass in Detail p86 Assembly Language: 1st Pass in Detail
Memory Allocation: for each instruction:
assumes allocation starts at address 0000H • allocate bytes needed to encode opcode and operands
uses location counter ($) to track address of next byte to • example: suppose $ = 0006H when next line of program is:
allocate MOV CL, BL
– requires 2 bytes to encode instruction: opcode, CL dest, BL source
as bytes are allocated, adjust $ value – after allocation: $ = 0008H
for each memory declaration: (DB, DW) if next line of program is:
• allocate # bytes declared, for example: suppose $ = 0006H MOV BX, 1
Instruction encoding
and next line of program is: DW – requires 4 bytes to encode details later
– will allocate 2 bytes for DW statement: – 2 bytes: opcode, BX dest, imm as src
» use address 0006H for low byte – 2 bytes: imm src value
» use address 0007H for high byte – after allocation: $ = 000CH
– after allocation: $ = 0008H (next byte to allocate) when a label definition is encountered:
• if next line of program is: DB ‘Hi Mom’ • value of label = $
– will allocate 6 bytes for DB statement – value of label is the address of the next byte allocated to the
» use address 0008H for ‘H’, …, 000DH for ‘m’ program
– after allocation: $ = 000EH • save (label, $-value) in Symbol Table for recall during 2nd pass
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 15 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 16
p86 Assembly Language: 2nd Pass in Detail p86 Assembly Language: HI.LST Example
build image of bytes to be loaded at addresses $ binary image Label definition: in 1st pass – put
0000 start: value in Symbol table
memory declaration: initialization value?
0000 C7C2E904 mov dx, 04E9H
• if no init value – use default? 0004 C6C048 mov al, 'H'
instructions: opcodes and operands 0007 EE out [dx], al
• addressing mode info 0008 C6C069 mov al, 'i'
• label references in operands: look up values to use for label 000B EE out [dx], al
in Symbol Table 000C F4 hlt
• for relative-offsets: offset = label-value – $ end start
After 1st Pass:
results to .LST file
syntax OK
• errors bytes have been allocated to statements
• image created (none if errors!) Symbol Table constructed:
.OBJ file contains image in format that can be loaded by Symbol Value
simulator start 0000H
• includes info about “start address” – to initialize IP during After 2 Pass:
nd
•3
p86 Assembly Language: ASSIGN2.LST Example p86 Assembly Language: ASSIGN2.LST Example
34 0000 Start: 60 0020 80ED01 sub ch, 1
35 0000 C7C4FEFF mov sp, 0FFFEH 61 0023 75ED jnz Outloop
37 0004 C7C2E904 mov dx, 04E9H Symbol Value
43 0008 C6C510 mov ch, 16
Start 0000H Note: Do not confuse $ with IP
Outloop 0012H $ = artifact of assembler
44 000B C6C101 mov cl, 1
Got0 0016H
45 000E 8B1EA300 mov bx, [Value] IP = artifact of processor
Got1 001CH
Dump 001FH
47 0012 Outloop:
316 009B 0A00 Ten: dw 10
48 0012 D3E3 shl bx, cl
317 009D 6400 Hundred: dw 100
49 0014 7206 jc Got1 318 009F E803 Thousand: dw 1000
50 0016 Got0: 319 00A1 1027 TenThousand: dw 10000
51 0016 C6C030 mov al, ‘0’
52 0019 E90300 jmp Dump 321 00A3 AF90 Value: dw 090AFH
54 001C Got1: Symbol Value
55 001C C6C031 mov al, ‘1’ Ten 009BH
Hundred 009DH
Thousand 009FH
57 001F Dump:
TenThousand 00A1H
58 001F EE out [dx], al
Value 00A3H
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 19 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 20
p86 Assembly Language: EQU Directive p86 Assembly Language: EQU Example
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 21 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 22
p86 Assembly Language: ORG Directive p86 Assembly Language: ORG Example
…
allows specification of the address of the next byte to be JMP DoAtF000H
allocated ; suppose this instruction was
improves (human) readability ; assembled to bytes starting at 0120H
ORG 0F000H
reduces the use of “magic numbers”
DoAtF000H:
MOV BX , . . .
Syntax: ; MOV instruction will be assembled
ORG numeric-constant ; to bytes starting at address F000H
Assembler assigns location counter the value of the specified Must always increase location counter (never decrease)
numeric constant Typical use:
• Force assembly to specific address range
• Resulting code will reside in particular type of memory
located at that address (e.g. ROM)
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 23 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 24
•4