TOY assembly Introduction to Computer Science Robert Sedgewick
TOY assembly Introduction to Computer Science • Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http: //www. cs. Princeton. EDU/Intro. CS
TOY assembly Not mapping to instruction u u u u u Data directives A DW n: initialize a variable A as n B DUP n: reserve n words (n is decimal) Support two types of literals, decimal and hexadecimal (0 x) Label begins with a letter Comment begins with ; Case insensitive Program starts with the first instruction it meets Some tricks to handle the starting address 0 x 10 opcode 0 1 2 3 4 5 6 7 8 9 A B C D E F mnemonic hlt add sub and xor shl shr lda ld st ldi sti bz bp jr jl syntax hlt add rd, rs, rt sub rd, rs, rt and rd, rs, rt xor rd, rs, rt shl rd, rs, rt shr rd, rs, rt lda rd, addr ld rd, addr st rd, addr ldi rd, rt sti rd, rt bz rd, addr bp rd, addr jr rd (rt) jl rd, addr 2
Assembler’s task: u u u Convert mnemonic operation codes to their machine language equivalents Convert symbolic operands to their equivalent machine addresses Build machine instructions in proper format Convert data constants into internal machine representations (data formats) Write object program and the assembly listing 3
Forward Reference Definition u A reference to a label that is defined later in the program Solution u Two passes First pass: scan the source program for label definition, address accumulation, and address assignment – Second pass: perform most of the actual instruction translation – 4
Assembly version of REVERSE int A[32]; A i=0; Do { read RD=stdin; if (RD==0) break; A[i]=RD; i=i+1; } while (1); printr(); exit DUP 32 10: C 020 lda lda R 1, 1 RA, A RC, 0 20: 7101 21: 7 A 00 22: 7 C 00 ld bz add sti add bz RD, 0 x. FF RD, exit R 2, RA, RC RD, R 2 RC, R 1 R 0, read 23: 8 DFF 24: CD 29 25: 12 AC 26: BD 02 27: 1 CC 1 28: C 023 jl hlt RF, printr 29: FF 2 B 2 A: 0000 5
Assembly version of REVERSE printr() { do { i=i-1; print A[i]; } while (i>=0); } return; ; print reverse ; array address (RA) ; number of elements (RC) printr sub RC, R 1 add R 2, RA, RC ldi RD, R 2 st RD, 0 x. FF bp RC, printr bz RC, printr return jr RF 2 B: 2 CC 1 2 C: 12 AC 2 D: AD 02 2 E: 9 DFF 2 F: DC 2 B 30: CC 2 B 31: EF 00 toyasm < reverse. asm > reverse. toy 6
stack STK_TOP DW 0 x. FF data ; these procedures will use R 8, R 9 ; assume return address is in RE, instead of RF ; it is the only exception ; push RF into stack push lda R 8, 1 ld R 9, STK_TOP sub R 9, R 8 st R 9, STK_TOP sti RF, R 9 jr RE code STK_TOP stack FE FF stdin/stdout 7
stack ; pop and return [top] to RF pop lda R 8, 0 x. FF ld R 9, STK_TOP sub R 8, R 9 bz R 8, popexit ldi RF, R 9 lda R 8, 1 add R 9, R 8 st R 9, STK_TOP popexit jr RE ; the size of the stack, the result is in R 9 stksize lda R 8, 0 x. FF ld R 9, STK_TOP sub R 9, R 8, R 9 jr RE 8
Procedure prototype With a stack, the procedure prototype is changed. It allows us to have a deeper call graph by using the stack. B mul A mul jl RE, push A A() code call B jr RF B() C() call C jl RE, pop jr RF A before after 9
Assembly programming flow source assembler object linker • Combine separate object codes • Supply necessary information for references between them executable loader Bring the object program into memory for execution Target machine 10
Linking Many programs will need multiply. Since multiply will be used by many applications, could we make multiply a library? Toyasm has an option to generate an object file so that it can be later linked with other object files. That is why we need linking. Write a subroutine mul 3 which multiplies three numbers in RA, RB, RC together and place the result in RD. Three files: stack. obj: implementation of stack, needed for procedure mul. obj: implementation of multiplication. multest. obj: main program and procedure of mul 3 u u u toylink multest. obj mul. obj stack. obj > multest. toy 11
object file (multest. obj) A B C DW DW DW 3 4 5 ; calculate A*B*C main ld RA, A ld RB, B ld RC, C jl RF, mul 3 st RD, 0 x. FF hlt ; RD=RA*RB*RC ; return address is in RF mul 3 jl RE, push lda add jl add RD, 0 RD, RC, R 0 RF, mul RA, RC, R 0 RB, RD, R 0 RF, mul RD, RC, R 0 jl jr RE, pop RF 12
object file (mul. obj) // size 29 // export 4 export // SIXTEEN 0 x 00 ; multiply RC=RA*RB table // mul 0 x 10 ; return address is in RF // m_loop 0 x 14 ; it will modify R 2, R 3 and R 4 as well // m_end 0 x 1 A mul jl RE, push // literal 2 17 18 // lines 14 These are literals. lda RC, 0 No need to relocate 00: 0010 need to fill in 10: FE 00 lda R 1, 1 address of push 11: 7 C 00 ld R 2, SIXTEEN 12: 7101 once we know it m_loop sub R 2, R 1 13: 8200 shl R 3, RA, R 2 14: 2221 shr R 4, RB, R 2 15: 53 A 2 and R 4, R 1 16: 64 B 2 bz R 4, m_end 17: 3441 add RC, R 3 18: C 41 A 19: 1 CC 3 m_end bp R 2, m_loop need to fill in 1 A: D 214 address of pop 1 B: FE 00 jl RE, pop once we know it 1 C: EF 00 jr RF // import 2 import // push 1 0 x 10 table // pop 1 0 x 1 B SIXTEEN DW 16 13
Linking multest. obj mul 0 x 10 29 32 push 0 x 10 pop 0 x 1 B start address =0+32=0 x 20 start address=0 0 x 00 0 x 20 stack. obj push 0 x 10 pop 0 x 16 0 x 3 D+0 x 10=0 x 4 D 0 x 3 D+0 x 16=0 x 53 35 start address =32+29=0 x 3 D 14
Resolve external symbols 20: 0010 30: FE 4 D 31: 7 C 00 32: 7101 33: 8220 34: 2221 35: 53 A 2 36: 64 B 2 37: 3441 38: C 43 A 39: 1 CC 3 3 A: D 234 3 B: FE 53 3 C: EF 20 // size 29 // export 4 export // SIXTEEN 0 x 00 table // mul 0 x 10 // m_loop 0 x 14 // m_end 0 x 1 A // literal 2 17 18 // lines 14 These are literals. No need to relocate 00: 0010 need to fill in 10: FE 00 address of push 11: 7 C 00 12: 7101 once we know it 13: 8200 14: 2221 15: 53 A 2 16: 64 B 2 17: 3441 18: C 41 A 19: 1 CC 3 need to fill in 1 A: D 214 address of pop 1 B: FE 00 once we know it 1 C: EF 00 // import 2 import // push 1 0 x 10 table // pop 1 0 x 1 B 15
Virtual machines Abstractions for computers compiler 16
Operating System Operating system is a resource allocator u Managing all resources (memory, I/O, execution) – Resolving requests for efficient and fair usage – Operating system is a control program u – Controlling execution of programs to prevent errors and improper use of the computer 17
Virtual machines Abstractions for computers assembler 18
Virtual machines Abstractions for computers architecture 19
Virtual machines Abstractions for computers DSD, electronics 20
- Slides: 20