ics lab4: y86 assembler
DESCRIPTION
ICS Lab4: Y86 Assembler. Introduction. Task. Y86 Assembler Input: y86 assembly file (e.g., asum.ys ) Output: y86 binary file (e.g. asum.bin ) Side effect: print readable image to screen Info Correct: nothing Failure: error message. # begin of asum.ys . pos 0 - PowerPoint PPT PresentationTRANSCRIPT
1
ICS Lab4: Y86 Assembler
Introduction
2
3
Task
• Y86 Assembler– Input: y86 assembly file (e.g., asum.ys)– Output: y86 binary file (e.g. asum.bin)– Side effect: print readable image to screen
– Info• Correct: nothing• Failure: error message
4
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
# Function SumSum: pushl %ebp
rrmovl %esp,%ebpmrmovl 8(%ebp),%ecxmrmovl 12(%ebp),%edxxorl %eax,%eaxandl %edx,%edxje End
Loop: mrmovl (%ecx),%esiaddl %esi,%eaxirmovl $4,%ebxaddl %ebx,%ecxirmovl $-1,%ebxaddl %ebx,%edxjne Loop
End: rrmovl %ebp,%esppopl %ebpret
.pos 0x100Stack: # end of asum.ys
5
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
# Function SumSum: pushl %ebp
rrmovl %esp,%ebpmrmovl 8(%ebp),%ecxmrmovl 12(%ebp),%edxxorl %eax,%eaxandl %edx,%edxje End
Loop: mrmovl (%ecx),%esiaddl %esi,%eaxirmovl $4,%ebxaddl %ebx,%ecxirmovl $-1,%ebxaddl %ebx,%edxjne Loop
End: rrmovl %ebp,%esppopl %ebpret
.pos 0x100Stack: # end of asum.ys
7
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
# Function SumSum: pushl %ebp
rrmovl %esp,%ebpmrmovl 8(%ebp),%ecxmrmovl 12(%ebp),%edxxorl %eax,%eaxandl %edx,%edxje End
Loop: mrmovl (%ecx),%esiaddl %esi,%eaxirmovl $4,%ebxaddl %ebx,%ecxirmovl $-1,%ebxaddl %ebx,%edxjne Loop
End: rrmovl %ebp,%esppopl %ebpret
.pos 0x100Stack: # end of asum.ys
comment
8
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
# Function SumSum: pushl %ebp
rrmovl %esp,%ebpmrmovl 8(%ebp),%ecxmrmovl 12(%ebp),%edxxorl %eax,%eaxandl %edx,%edxje End
Loop: mrmovl (%ecx),%esiaddl %esi,%eaxirmovl $4,%ebxaddl %ebx,%ecxirmovl $-1,%ebxaddl %ebx,%edxjne Loop
End: rrmovl %ebp,%esppopl %ebpret
.pos 0x100Stack: # end of asum.ys
directive
9
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
# Function SumSum: pushl %ebp
rrmovl %esp,%ebpmrmovl 8(%ebp),%ecxmrmovl 12(%ebp),%edxxorl %eax,%eaxandl %edx,%edxje End
Loop: mrmovl (%ecx),%esiaddl %esi,%eaxirmovl $4,%ebxaddl %ebx,%ecxirmovl $-1,%ebxaddl %ebx,%edxjne Loop
End: rrmovl %ebp,%esppopl %ebpret
.pos 0x100Stack: # end of asum.ys
Symbol
10
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
# Function SumSum: pushl %ebp
rrmovl %esp,%ebpmrmovl 8(%ebp),%ecxmrmovl 12(%ebp),%edxxorl %eax,%eaxandl %edx,%edxje End
Loop: mrmovl (%ecx),%esiaddl %esi,%eaxirmovl $4,%ebxaddl %ebx,%ecxirmovl $-1,%ebxaddl %ebx,%edxjne Loop
End: rrmovl %ebp,%esppopl %ebpret
.pos 0x100Stack: # end of asum.ys
Relocation
11
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
12
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
| # begin of asum.ys 0x000: | .pos 0 0x000: 30f400010000 | init: irmovl Stack,
%esp 0x006: 30f500010000 | irmovl Stack, %ebp 0x00c: 8024000000 | call Main 0x011: 00 | halt | 0x014: | .align 4 0x014: 0d000000 | array:.long 0xd 0x018: c0000000 | .long 0xc0 0x01c: 000b0000 | .long 0xb00 0x020: 00a00000 | .long 0xa000 | # Function Main 0x024: a05f | Main: pushl %ebp 0x026: 2045 | rrmovl %esp,%ebp 0x028: 30f004000000 | irmovl $4,%eax 0x02e: a00f | pushl %eax 0x030: 30f214000000 | irmovl array,%edx 0x036: a02f | pushl %edx 0x038: 8042000000 | call Sum 0x03d: 2054 | rrmovl %ebp,%esp 0x03f: b05f | popl %ebp 0x041: 90 | ret
Readable Dump
13
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
| # begin of asum.ys 0x000: | .pos 0 0x000: 30f400010000 | init: irmovl Stack,
%esp 0x006: 30f500010000 | irmovl Stack, %ebp 0x00c: 8024000000 | call Main 0x011: 00 | halt | 0x014: | .align 4 0x014: 0d000000 | array:.long 0xd 0x018: c0000000 | .long 0xc0 0x01c: 000b0000 | .long 0xb00 0x020: 00a00000 | .long 0xa000 | # Function Main 0x024: a05f | Main: pushl %ebp 0x026: 2045 | rrmovl %esp,%ebp 0x028: 30f004000000 | irmovl $4,%eax 0x02e: a00f | pushl %eax 0x030: 30f214000000 | irmovl array,%edx 0x036: a02f | pushl %edx 0x038: 8042000000 | call Sum 0x03d: 2054 | rrmovl %ebp,%esp 0x03f: b05f | popl %ebp 0x041: 90 | ret
Readable Dump
14
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
| # begin of asum.ys 0x000: | .pos 0 0x000: 30f400010000 | init: irmovl Stack,
%esp 0x006: 30f500010000 | irmovl Stack, %ebp 0x00c: 8024000000 | call Main 0x011: 00 | halt | 0x014: | .align 4 0x014: 0d000000 | array:.long 0xd 0x018: c0000000 | .long 0xc0 0x01c: 000b0000 | .long 0xb00 0x020: 00a00000 | .long 0xa000 | # Function Main 0x024: a05f | Main: pushl %ebp 0x026: 2045 | rrmovl %esp,%ebp 0x028: 30f004000000 | irmovl $4,%eax 0x02e: a00f | pushl %eax 0x030: 30f214000000 | irmovl array,%edx 0x036: a02f | pushl %edx 0x038: 8042000000 | call Sum 0x03d: 2054 | rrmovl %ebp,%esp 0x03f: b05f | popl %ebp 0x041: 90 | ret
Readable Dump
15
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
| # begin of asum.ys 0x000: | .pos 0 0x000: 30f400010000 | init: irmovl Stack,
%esp 0x006: 30f500010000 | irmovl Stack, %ebp 0x00c: 8024000000 | call Main 0x011: 00 | halt | 0x014: | .align 4 0x014: 0d000000 | array:.long 0xd 0x018: c0000000 | .long 0xc0 0x01c: 000b0000 | .long 0xb00 0x020: 00a00000 | .long 0xa000 | # Function Main 0x024: a05f | Main: pushl %ebp 0x026: 2045 | rrmovl %esp,%ebp 0x028: 30f004000000 | irmovl $4,%eax 0x02e: a00f | pushl %eax 0x030: 30f214000000 | irmovl array,%edx 0x036: a02f | pushl %edx 0x038: 8042000000 | call Sum 0x03d: 2054 | rrmovl %ebp,%esp 0x03f: b05f | popl %ebp 0x041: 90 | ret
Readable Dump
relocation
16
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret
| # begin of asum.ys 0x000: | .pos 0 0x000: 30f400010000 | init: irmovl Stack,
%esp 0x006: 30f500010000 | irmovl Stack, %ebp 0x00c: 8024000000 | call Main 0x011: 00 | halt | 0x014: | .align 4 0x014: 0d000000 | array: .long 0xd 0x018: c0000000 | .long 0xc0 0x01c: 000b0000 | .long 0xb00 0x020: 00a00000 | .long 0xa000 | # Function Main 0x024: a05f | Main: pushl %ebp 0x026: 2045 | rrmovl %esp,%ebp 0x028: 30f004000000 | irmovl $4,%eax 0x02e: a00f | pushl %eax 0x030: 30f214000000 | irmovl array,%edx 0x036: a02f | pushl %edx 0x038: 8042000000 | call Sum 0x03d: 2054 | rrmovl %ebp,%esp 0x03f: b05f | popl %ebp 0x041: 90 | ret
Readable Dump
relocation
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret 17
| # begin of asum.ys 0x000: | .pos 0 0x000: 30f400010000 | init: irmovl Stack,
%esp 0x006: 30f500010000 | irmovl Stack, %ebp 0x00c: 8024000000 | call Main 0x011: 00 | halt | 0x014: | .align 4 0x014: 0d000000 | array: .long 0xd 0x018: c0000000 | .long 0xc0 0x01c: 000b0000 | .long 0xb00 0x020: 00a00000 | .long 0xa000 | # Function Main 0x024: a05f | Main: pushl %ebp 0x026: 2045 | rrmovl %esp,%ebp 0x028: 30f004000000 | irmovl $4,%eax 0x02e: a00f | pushl %eax 0x030: 30f214000000 | irmovl array,%edx 0x036: a02f | pushl %edx 0x038: 8042000000 | call Sum 0x03d: 2054 | rrmovl %ebp,%esp 0x03f: b05f | popl %ebp 0x041: 90 | ret
30f4 0001 0000 30f5 0001 0000 8024 0000 0000 0000 0d00 0000 c000 0000 000b 0000 00a0 0000 a05f 2045 30f0 0400 0000 a00f 30f2 1400 0000 a02f 8042 0000 0020 54b05f90
Readable Dump
Binary Code
# begin of asum.ys.pos 0
init: irmovl Stack, %espirmovl Stack, %ebpcall Mainhalt
.align 4array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000# Function MainMain: pushl %ebp
rrmovl %esp,%ebpirmovl $4,%eaxpushl %eaxirmovl array,%edxpushl %edxcall Sumrrmovl %ebp,%esppopl %ebpret 18
| # begin of asum.ys 0x000: | .pos 0 0x000: 30f400010000 | init: irmovl Stack,
%esp 0x006: 30f500010000 | irmovl Stack, %ebp 0x00c: 8024000000 | call Main 0x011: 00 | halt | 0x014: | .align 4 0x014: 0d000000 | array: .long 0xd 0x018: c0000000 | .long 0xc0 0x01c: 000b0000 | .long 0xb00 0x020: 00a00000 | .long 0xa000 | # Function Main 0x024: a05f | Main: pushl %ebp 0x026: 2045 | rrmovl %esp,%ebp 0x028: 30f004000000 | irmovl $4,%eax 0x02e: a00f | pushl %eax 0x030: 30f214000000 | irmovl array,%edx 0x036: a02f | pushl %edx 0x038: 8042000000 | call Sum 0x03d: 2054 | rrmovl %ebp,%esp 0x03f: b05f | popl %ebp 0x041: 90 | ret
30f4 0001 0000 30f5 0001 0000 8024 0000 0000 0000 0d00 0000 c000 0000 000b 0000 00a0 0000 a05f 2045 30f0 0400 0000 a00f 30f2 1400 0000 a02f 8042 0000 0020 54b05f90
Readable Dump
Binary Code
19
Intro
• Y86 Assembler– Source code: y86asm.c & y86asm.h– ./y86asm -v asum.ys > asum.yo
• asum.ys: assembly code• asum.bin: binary code• asum.yo: readable dump
– Test-suite: simple, error and apps– Base-system: y86asm-base
– Requirement: the same output! (bin & yo)
Framework
20
assemble
relocate
line by line
Symbol table
Relocatetable
binfile
asm code pushl %eax call Sum irmovl $4,%eax
bin code 30f4 0001 0000 30f5 0001 0000 8024 0000 0000
screen 30f4 | pushl .. 30f5 | pushl .. 8024 | call ..
-v
done
int main(int argc, char *argv[]){ /* option (-v) */ init(); /* init */
/* open and assemble .ys file */ assemble(in);
/* relocate binary code */ relocate();
/* create and generate .bin file */ binfile(out);
/* print to screen (.yo file) */ if (screen) print_screen();
finit(); /* finit */ return 0;}
22
int main(int argc, char *argv[]){ /* option (-v) */ init(); /* init */
/* open and assemble .ys file */ assemble(in);
/* relocate binary code */ relocate();
/* create and generate .bin file */ binfile(out);
/* print to screen (.yo file) */ if (screen) print_screen();
finit(); /* finit */ return 0;}
23
int assemble(FILE *in){ while(fget(..) != NULL); { /* new line and fill it */ line = malloc(..); line->y86asm = ...;
/* parse the line */ parse_line(line); } return 0;}
int main(int argc, char *argv[]){ /* option (-v) */ init(); /* init */
/* open and assemble .ys file */ assemble(in);
/* relocate binary code */ relocate();
/* create and generate .bin file */ binfile(out);
/* print to screen (.yo file) */ if (screen) print_screen();
finit(); /* finit */ return 0;}
24
int relocate(void){ tmp = reltab; while(tmp != NULL); { /* find symbol */ /* fill address */
/* next */ tmp = tmp->next; } return 0;}
int main(int argc, char *argv[]){ /* option (-v) */ init(); /* init */
/* open and assemble .ys file */ assemble(in);
/* relocate binary code */ relocate();
/* create and generate .bin file */ binfile(out);
/* print to screen (.yo file) */ if (screen) print_screen();
finit(); /* finit */ return 0;}
25
int binfile(void){ /* prepare image */ image = malloc(..);
/* fill binary code */
/* write output file */ return 0;}
int main(int argc, char *argv[]){ /* option (-v) */ init(); /* init */
/* open and assemble .ys file */ assemble(in);
/* relocate binary code */ relocate();
/* create and generate .bin file */ binfile(out);
/* print to screen (.yo file) */ if (screen) print_screen();
finit(); /* finit */ return 0;}
26
Implemented (good example)
Comment: ‘#’ => skip rest
Label: ‘:’ => add to symtab => continue
Instruction: keyword => fill codes => continue
keyword in+op rest byteshalt 0:0 1nop 1:0 1rrmovl 2:0 regA, regB 2cmovXX 2:N regA, regB 2irmovl 3:0 imm, regB 6rmmovl 4:0 regA, D(regB) 6mrmovl 5:0 D(regB), regA 6OPL 6:N regA, regB 2jXX 7:N Label 5call 8:0 Label 5ret 9:0 1pushl a:0 regA 2popl b:0 regA 2directive: ‘.’ .pos - digit 0 .align - digit 0 .l|w|b - D 4|2|1
error
A line of assembly code
28
typedef struct symbol { char *name; int addr; struct symbol *next;} symbol_t;
line_t *y86bin_listhead; /* head */line_t *y86bin_listtail; /* tail */int y86asm_lineno;int vmaddr; /* address */
symbol_t *symtab;reloc_t *reltab;
typedef struct line { type_t type; /* TYPE_COMM: no y86bin, TYPE_INS: both y86bin and y86asm */ bin_t y86bin; char *y86asm; struct line *next;} line_t;
typedef struct bin { int addr; byte_t codes[6]; int bytes;} bin_t;
typedef enum{ TYPE_COMM, TYPE_INS, TYPE_ERR } type_t;
typedef struct reloc { bin_t *y86bin; char *name; struct reloc *next;} reloc_t;
# nop, nop and haltnopnophalt # Terminate program
# end
29
nop.ys
./y86asm –v nop.ys > nop.yo
make nop.yoor
| # nop, nop and halt 0x000: 10 | nop 0x001: 10 | nop 0x002: 00 | halt # Terminate program | # end
nop.yo
NO “nop.bin” now
Testbed
30*Acknowledgement: Li Cheng, Yuchen Liu
31
Resource
• Files– y86asm.c/y86asm.h (svn commit)– Makefile– yat/yat.c
– y86-base: y86asm-base, Makefile, {ins}.ys, {app}.ys
– y86-ins: Makefile, {ins}.ys– y86-err: {err}.ys– y86-app: {app}.ys
32
Tools
• Makefile– make - compiling y86asm– make clean - remove y86asm, *.o, *.yo, …– make yat - compiling yat– make **.yo - generate ??.yo file by y86asm
– y86-base/make - generate yo files foreach
33
Tools
• yatOption specification: -s test single instruction ./y86-ins/<name>.ys, or error-handling case in ./y86-err/<name>.ys e.g. yat -s rrmovl, yat -s symbol-error -S test both instructions and error-handling -a test single application ./y86-app/<name>.ys -A test the application codes in ./y86-app -F test instructions, error-handling and application codes, and you will get a total score -h print this message
Usage: yat -s <name> Or: yat -S Or: yat -a <name> Or: yat -A Or: yat -F
34
Tools
• yatOption specification: -s test single instruction ./y86-ins/<name>.ys, or error-handling case in ./y86-err/<name>.ys e.g. yat -s rrmovl, yat -s symbol-error -S test both instructions and error-handling -a test single application ./y86-app/<name>.ys -A test the application codes in ./y86-app -F test instructions, error-handling and application codes, and you will get a total score -h print this message
Usage: yat -s <name> Or: yat -S Or: yat -a <name> Or: yat -A Or: yat -F
Score for instructions: 32.00/32.00Score for error-handling cases: 8.00/ 8.00Score for applications: 40.00/40.00Total score: 80.00/80.00
35
$./yat –s halt[ Testing instruction: halt ]Binary files halt.bin.base and halt.bin differ[ Result: Fail ]Score for instructions: 0.00/ 1.00
$./yat –s addl[ Testing instruction: addl ][L2]: Invalid instr[L2]: Assemble y86 code errormake: *** [addl.yo] Error 1[ Result: Fail ]Score for instructions: 0.00/ 1.00
36
Cheating will not be tolerated All homework, labs and exams are to be done individually.
It is your responsibility to ensure that “your passwords are well-guarded, directories protected, and printouts do not fall into other hands”.