You are on page 1of 4

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

p86 Assembly Language: Constants p86 Assembly Language: Labels


binary value: consists of only 0’s and 1’s ƒ user-defined names – represent addresses
ƒ ends with ‘B’ or ‘b’, e.g. 10101110b ƒ lets programmer refer to addresses using logical names
– no need for concern with exact hexadecimal values
hexadecimal value: starts with 0 .. 9 ƒ leave assembler to:
ƒ may include 0 . . 9, A .. F (a . . f ) • decide exact addresses to use
ƒ ends with ‘H’ or ‘h’, e.g. 0FFH (8-bit hex value) • deal with hexadecimal addresses
decimal value: default format – no “qualifier” extension ƒ labels are used to identify addresses for:
ƒ consists of digits in 0 . . 9, e.g. 12345 • control flow – identify address of target
• memory variables – identify address where data is
string: sequence of characters encoded as 7-bit ASCII stored
bytes: ƒ labels serve in 2 roles:
ƒ enclosed in single quotes, e.g. ‘Hi Mom’ (6 bytes) • label definition and label reference
ƒ character: string with length = 1
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 5 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 6

•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

p86 Assembly Language: Instructions p86 Assembly Language: Instructions


ƒ complete instruction must be on one line ƒ Operands (continued)
ƒ instruction mnemonic & operands • register indirect: [ register ]
ƒ operands – example
• immediate: constant W: DW
...
– can use label reference, example:
MOV BX, W
W: DW
MOV AX, [ BX ]
...
MOV BX, W • relative-offset (control flow)
• register: register name – use label reference to identify target address
– assembler calculates actual offset
• direct: [ address ] JMP There
– state address a label reference, example:
...
W: DW
There:
...
etc.
MOV AX, [ W ]
30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 11 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 12

•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

loading ƒ binary image constructed


30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 17 30-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 18

•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

ƒ allows declaration of symbolic constants DisplayPort EQU 04E9H ; magic number


ƒ improves (human) readability
ƒ reduces the use of “magic numbers”
MOV DX , DisplayPort
Note: No “:”
Syntax: ; improved readability
symbolic-name EQU numeric-constant

ƒ 1st Pass: records (symbolic-name, constant) in symbol table


ƒ is assembled to same encoding as
MOV DX , 04E9H
ƒ 2nd Pass: replaces every occurrence of the symbolic-name with
the specified constant
• is NOT allocated any bytes !!!

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

You might also like