1 / 36

ICS Lab4: Y86 Assembler

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

anakin
Download Presentation

ICS Lab4: Y86 Assembler

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICS Lab4: Y86 Assembler

  2. Introduction

  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, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret # Function Sum Sum: pushl %ebp rrmovl %esp,%ebp mrmovl8(%ebp),%ecx mrmovl12(%ebp),%edx xorl%eax,%eax andl%edx,%edx je End Loop: mrmovl (%ecx),%esi addl%esi,%eax irmovl$4,%ebx addl%ebx,%ecx irmovl$-1,%ebx addl%ebx,%edx jne Loop End: rrmovl %ebp,%esp popl%ebp ret .pos 0x100 Stack: # end of asum.ys

  5. # begin of asum.ys .pos 0 init: irmovl Stack, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret # Function Sum Sum: pushl %ebp rrmovl %esp,%ebp mrmovl8(%ebp),%ecx mrmovl12(%ebp),%edx xorl%eax,%eax andl%edx,%edx je End Loop: mrmovl (%ecx),%esi addl%esi,%eax irmovl$4,%ebx addl%ebx,%ecx irmovl$-1,%ebx addl%ebx,%edx jne Loop End: rrmovl %ebp,%esp popl%ebp ret .pos 0x100 Stack: # end of asum.ys

  6. # begin of asum.ys .pos 0 init: irmovl Stack, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret # Function Sum Sum: pushl %ebp rrmovl %esp,%ebp mrmovl8(%ebp),%ecx mrmovl12(%ebp),%edx xorl%eax,%eax andl%edx,%edx je End Loop: mrmovl (%ecx),%esi addl%esi,%eax irmovl$4,%ebx addl%ebx,%ecx irmovl$-1,%ebx addl%ebx,%edx jne Loop End: rrmovl %ebp,%esp popl%ebp ret .pos 0x100 Stack: # end of asum.ys comment

  7. # begin of asum.ys .pos 0 init: irmovl Stack, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret # Function Sum Sum: pushl %ebp rrmovl %esp,%ebp mrmovl8(%ebp),%ecx mrmovl12(%ebp),%edx xorl%eax,%eax andl%edx,%edx je End Loop: mrmovl (%ecx),%esi addl%esi,%eax irmovl$4,%ebx addl%ebx,%ecx irmovl$-1,%ebx addl%ebx,%edx jne Loop End: rrmovl %ebp,%esp popl%ebp ret .pos 0x100 Stack: # end of asum.ys directive

  8. # begin of asum.ys .pos 0 init:irmovl Stack, %esp irmovlStack, %ebp call Main halt .align 4 array:.long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main:pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret # Function Sum Sum:pushl %ebp rrmovl %esp,%ebp mrmovl8(%ebp),%ecx mrmovl12(%ebp),%edx xorl%eax,%eax andl%edx,%edx je End Loop:mrmovl (%ecx),%esi addl%esi,%eax irmovl$4,%ebx addl%ebx,%ecx irmovl$-1,%ebx addl%ebx,%edx jne Loop End:rrmovl %ebp,%esp popl%ebp ret .pos 0x100 Stack: # end of asum.ys Symbol

  9. # begin of asum.ys .pos0 init:irmovlStack, %esp irmovlStack, %ebp call Main halt .align 4 array:.long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main:pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret # Function Sum Sum:pushl %ebp rrmovl %esp,%ebp mrmovl8(%ebp),%ecx mrmovl12(%ebp),%edx xorl%eax,%eax andl %edx,%edx je End Loop:mrmovl (%ecx),%esi addl%esi,%eax irmovl$4,%ebx addl%ebx,%ecx irmovl$-1,%ebx addl%ebx,%edx jneLoop End:rrmovl %ebp,%esp popl%ebp ret .pos0x100 Stack: # end of asum.ys Relocation

  10. # begin of asum.ys .pos0 init:irmovlStack, %esp irmovlStack, %ebp call Main halt .align 4 array:.long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main:pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret

  11. # begin of asum.ys .pos 0 init: irmovl Stack, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret | # 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

  12. # begin of asum.ys .pos 0 init: irmovl Stack, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret | # 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, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret | # 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, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret | # begin of asum.ys 0x000: | .pos 0 0x000: 30f400010000 |init:irmovlStack, %esp 0x006: 30f500010000 |irmovlStack, %ebp 0x00c: 8024000000 |callMain 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|irmovlarray,%edx 0x036: a02f | pushl %edx 0x038: 8042000000|callSum 0x03d: 2054 | rrmovl %ebp,%esp 0x03f: b05f | popl %ebp 0x041: 90 | ret Readable Dump relocation

  15. # begin of asum.ys .pos 0 init: irmovl Stack, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret | # begin of asum.ys 0x000: | .pos 0 0x000: 30f400010000 |init:irmovlStack, %esp 0x006: 30f500010000 |irmovlStack, %ebp 0x00c: 8024000000 | callMain 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|irmovlarray,%edx 0x036: a02f | pushl %edx 0x038: 8042000000|callSum 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, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret |# 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 Binary Code 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 54b0 5f90

  17. # begin of asum.ys .pos 0 init: irmovl Stack, %esp irmovlStack, %ebp call Main halt .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 # Function Main Main: pushl %ebp rrmovl%esp,%ebp irmovl$4,%eax pushl%eax irmovlarray,%edx pushl%edx call Sum rrmovl%ebp,%esp popl%ebp ret |# 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 Binary Code 30f4 0001 0000 30f5 0001 0000 8024 0000 000000000d00 0000 c000 0000 000b 0000 00a0 0000 a05f 2045 30f0 0400 0000 a00f 30f2 1400 0000 a02f 8042 0000 0020 54b0 5f90

  18. 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)

  19. Framework

  20. asm code pushl%eax call Sum irmovl$4,%eax assemble line by line Symbol table Relocatetable • screen • 30f4 | pushl .. 30f5 | pushl .. • 8024 | call .. relocate • bin code • 30f4 0001 0000 30f5 0001 0000 • 8024 0000 0000 binfile print -v done

  21. int main(intargc, 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(intargc, 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; } int assemble(FILE *in) { while(fget(..) != NULL); { /* new line and fill it */ line = malloc(..); line->y86asm = ...; /* parse the line */ parse_line(line); } return 0; }

  23. int main(intargc, 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; } intrelocate(void) { tmp = reltab; while(tmp != NULL); { /* find symbol */ /* fill address */ /* next */ tmp = tmp->next; } return 0; }

  24. int main(intargc, 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; } intbinfile(void) { /* prepare image */ image = malloc(..); /* fill binary code */ /* write output file */ return 0; }

  25. int main(intargc, 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; } Implemented (good example)

  26. A line of assembly code Comment: ‘#’ => skip rest keywordin+oprestbytes halt 0:0 1 nop 1:0 1 rrmovl 2:0regA, regB2 cmovXX 2:NregA, regB 2 irmovl 3:0imm, regB 6 rmmovl 4:0regA, D(regB) 6 mrmovl 5:0 D(regB), regA 6 OPL 6:NregA, regB 2 jXX 7:NLabel 5 call 8:0Label 5 ret 9:0 1 pushl a:0regA 2 popl b:0regA 2 directive: ‘.’ .pos - digit 0 .align - digit 0 .l|w|b - D 4|2|1 Label: ‘:’ => add to symtab => continue Instruction: keyword => fill codes => continue error

  27. line_t *y86bin_listhead; /* head */ line_t *y86bin_listtail; /* tail */ inty86asm_lineno; intvmaddr; /* address */ typedefenum{ TYPE_COMM, TYPE_INS, TYPE_ERR } type_t; typedefstruct line { type_ttype; /* TYPE_COMM: no y86bin, TYPE_INS: both y86bin and y86asm */ bin_ty86bin; char *y86asm; struct line *next; } line_t; typedefstruct bin { intaddr; byte_tcodes[6]; intbytes; } bin_t; symbol_t *symtab; reloc_t *reltab; typedefstruct symbol { char *name; intaddr; struct symbol *next; } symbol_t; typedefstructreloc { bin_t *y86bin; char *name; structreloc *next; } reloc_t;

  28. nop.ys # nop, nop and halt nop nop halt # Terminate program # end ./y86asm –v nop.ys > nop.yo or make nop.yo NO “nop.bin” now nop.yo | # nop, nop and halt 0x000: 10 | nop 0x001: 10 | nop 0x002: 00 | halt # Terminate program | # end

  29. Testbed *Acknowledgement: Li Cheng, Yuchen Liu

  30. 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

  31. 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

  32. Tools Usage: yat -s <name> Or: yat -S Or: yat -a <name> Or: yat -A Or: yat -F • yat Option 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

  33. Tools Usage: yat -s <name> Or: yat -S Or: yat -a <name> Or: yat -A Or: yat -F • yat Option 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 Score for instructions: 32.00/32.00 Score for error-handling cases: 8.00/ 8.00 Score for applications: 40.00/40.00 Total score: 80.00/80.00

  34. $./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 error make: *** [addl.yo] Error 1 [ Result: Fail ] Score for instructions: 0.00/ 1.00

  35. Cheatingwill not be tolerated All homework, labsand exams are to be done individually. It is your responsibilityto ensure that “your passwords are well-guarded, directories protected, and printouts do not fall into other hands”.

More Related