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