COP 3402 Systems Software Euripides Montagne University of

  • Slides: 19
Download presentation
COP 3402 Systems Software Euripides Montagne University of Central Florida (Spring 2008)

COP 3402 Systems Software Euripides Montagne University of Central Florida (Spring 2008)

COP 3402 Systems Software Assemblers

COP 3402 Systems Software Assemblers

ISA Instruction descriptions opcode mnemonic meaning 0001 0010 0011 0100 0101 0110 0111 1000

ISA Instruction descriptions opcode mnemonic meaning 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 0011 LOAD <x> ADD <x> STORE <x> SUB <x> IN <Device_#> OUT <Device_#> HALT JMP <x> SKIPZ SKIPG SKIPN A Mem[x] A A + Mem[x] A A A – Mem[x] A read from Device A output to Device Stop PC x If Z = 1 Skip next instruction If G = 1 Skip next instruction If L = 1 Skip next instruction

Assembly language Programming examples Assign a memory location to each variable: C X +

Assembly language Programming examples Assign a memory location to each variable: C X + Y; <000> <001> <002> If necessary to use temporary memory locations, assign labels (names) to them.

Assembly language Programming examples Memory 000 1245 001 1755 002 0000 After execution 003

Assembly language Programming examples Memory 000 1245 001 1755 002 0000 After execution 003 Load <000> 004 Add <001> 005 Store <002> 006 Halt Memory 000 1245 001 1755 002 3000 003 Load <000> 004 Add <001> 005 Store <002> 006 Halt

One Address Architecture • The instruction format of this one-address architecture consists of 16

One Address Architecture • The instruction format of this one-address architecture consists of 16 bits: 4 bits to represent instructions and 12 bits for addresses : OP 0001 ADDRESS 0000 0001

Assembler: translate Symbolic code to object code(binary) Assembly Language 003 Load <000> 004 Add

Assembler: translate Symbolic code to object code(binary) Assembly Language 003 Load <000> 004 Add <001> 005 Store <002> 006 Halt Assembler 01 LOAD 02 ADD 03 STORE 04 SUB 05 IN 003 004 005 006 06 OUT 07 HALT 08 JMP 09 SKIPZ In binary 0001 0000 0010 0000 0001 0011 00000011 0111 0000000

Assembler Directives • The next step to improve our assembly language is the incorporation

Assembler Directives • The next step to improve our assembly language is the incorporation of pseudo-ops (assembler directives) to invoke a special service from the assembler (pseudo-operations do not generate code). begin tell the assembler where the program starts . data to reserve a memory location. . end tells the assembler where the program ends. Labels are symbolic names used to identify memory locations.

Assembler Directives This is an example of the usage of assembler directives. begin “Assembly

Assembler Directives This is an example of the usage of assembler directives. begin “Assembly language instructions” halt (return to OS). data (to reserve a memory location). end ( tells the assembler where the program ends) note: the directive. end can be used to indicate where the program Starts (for eample: “. end <insert label here>”

Assembly language Programming Example 1 Label start a b TWO opcode address. begin in

Assembly language Programming Example 1 Label start a b TWO opcode address. begin in x 005 store a in x 005 store b load a sub TWO add b out x 009 halt. data 0. data 2. end start Text section (code) Data section

Assembly language Programming Example 2 Label 01 02 03 04 05 06 07 08

Assembly language Programming Example 2 Label 01 02 03 04 05 06 07 08 09 0 A 0 B 0 C 0 D 0 E 0 F 10 11 12 start sum a b one opcode address ; This is ; a comment. begin x 200 LOAD sum ADD a STORE sum LOAD b SUB one STORE b SKIPZ JMP here LOAD sum HALT. data x 000. data x 005. data x 003. data x 001. end start This program is computing 5 x 3.

ASSEMBLER Pass 1 Label 01 02 03 04 05 06 07 08 09 0

ASSEMBLER Pass 1 Label 01 02 03 04 05 06 07 08 09 0 A 0 B 0 C 0 D 0 E 0 F 10 11 12 start here sum a b one opcode address ; This is ; a comment. begin x 200 LOAD sum ADD a STORE sum LOAD b SUB one STORE b SKIPZ JMP here LOAD sum HALT. data x 000. data x 005. data x 003. data x 001. end start Symbol Table x 200 x 201 x 202 x 203 x 204 x 205 x 206 x 207 x 208 x 209 x 20 A x 20 B x 20 C x 20 D x 20 E here x 200 sum x 20 B a x 20 C b x 20 D one x 20 F symbol address In pass one the assembler examines the program line by line in order to built the symbol table. There is an entry in the symbol table for each label found in the program.

Opcode and Symbol Tables Opcode table opcode 0001 0010 0011 0100 0101 0110 0111

Opcode and Symbol Tables Opcode table opcode 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 0011 mnemonic LOAD ADD STOR SUB IN OUT HALT JMP SKIPZ SKIPG SKIPN Symbol Table here x 200 sum x 20 B a x 20 C b x 20 D one x 20 F symbol address Using the symbol table and the opcode table the assembler translates the program to object code. As the program can be loaded anywhere in memory PC-relative addressing is used to resolve the symbols. For instance, the offset between LOAD sum and the declaration of sum is 9, because when LOAD sum is fetched for execution, the pc is pointing to the instruction ADD a. ( pc + offset = 9)

ASSEMBLER Pass 2 Label 01 02 03 04 05 06 07 08 09 0

ASSEMBLER Pass 2 Label 01 02 03 04 05 06 07 08 09 0 A 0 B 0 C 0 D 0 E 0 F 10 11 12 start here sum a b one opcode address ; This is ; a comment. begin x 200 LOAD sum ADD a STORE sum LOAD b SUB one STORE b SKIPZ JMP here LOAD sum HALT. data x 000. data x 005. data x 003. data x 001. end start Object code PC o f f s e t x 200 x 201 x 202 x 203 x 204 x 205 x 206 x 207 x 208 x 209 x 20 A x 20 B x 20 C x 20 D x 20 E 000100001001 (9 is the offset) 001000001001 001100000111 (7 is the offset) 000100001000001000 001100000110 All addresses are pc-relative addresses. (PC + offset) Recall: PC is always pointing to the next instruction to be fetch.

Assembly language Programming object code Label 01 02 03 04 05 06 07 08

Assembly language Programming object code Label 01 02 03 04 05 06 07 08 09 0 A 0 B 0 C 0 D 0 E 0 F 10 11 12 start here sum a b one opcode address ; This is ; a comment. begin x 200 LOAD sum ADD a STORE sum LOAD b SUB one STORE b SKIPZ JMP here LOAD sum HALT. data x 000. data x 005. data x 003. data x 001. end start Object code PC o f f s e t x 200 x 201 x 202 x 203 x 204 x 205 x 206 x 207 x 208 x 209 x 20 A x 20 B x 20 C x 20 D x 20 E 000100001001 (9 is the offset) 001000001001 001100000111 (7 is the offset) 000100001000001000 001100000110 1001000000 100011111000 (-7) 00010000001 0111000000000000101 000000011 000000001 One’s complement

ASSEMBLER object code The object code file has several sections: Header section: Size of

ASSEMBLER object code The object code file has several sections: Header section: Size of code, name source file, size of data Text section (code): Object code Data section: Data (in binary) Relocation information section: Addresses to be fixed up by the linker Symbol table section: Global symbols in the program, Imported symbols Debugging section: Source file and line number information, description of data structures.

ASSEMBLER object code file for the example Program name: start Starting address text: x

ASSEMBLER object code file for the example Program name: start Starting address text: x 200 Length of text in bytes: x 14 Starting address data: x 20 A Length of data in bytes: 8 000100001001 001000001001 001100000111 000100001000001000 001100000110 1001000000 100011111000 00010000001 0111000000000000101 000000011 000000001 Header Text section Data section

Loading Object code in Memory Process Object code file Program name: start Starting address

Loading Object code in Memory Process Object code file Program name: start Starting address text: x 200 Length of text in bytes: x 14 Starting address data: x 20 A Length of data in bytes: 8 000100001001 001000001001 001100000111 000100001000001000 001100000110 1001000000 100011111000 00010000001 0111000000000000101 000000011 000000001 Header Text Data Text section Heap Data section Stack Run time environment

UNIX a. out format a. out object code format a. out header text section

UNIX a. out format a. out object code format a. out header text section data section symbol table information relocation Information a. out header magic number text segment size initialized data size(data) uninitialized data size(bss) symbol table size entry point text relocation size data relocation size magic number indicates type of executable file. bss is an acronym for block storage start. entry point: starting address of the program