Difference between revisions of "CSC231 Homework 2 2015"
(→Method 1) |
|||
(10 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
<br /> | <br /> | ||
* ssh to aurora with your 231a-xx account. | * ssh to aurora with your 231a-xx account. | ||
− | * get 3 programs from my | + | * get 3 programs from my account on aurora: |
wget http://cs.smith.edu/~dthiebaut/handout/hw2_1 | wget http://cs.smith.edu/~dthiebaut/handout/hw2_1 | ||
Line 19: | Line 19: | ||
wget http://cs.smith.edu/~dthiebaut/handout/hw2_3 | wget http://cs.smith.edu/~dthiebaut/handout/hw2_3 | ||
− | * | + | * You should now have 3 new files in your directory, called hw2_1, hw2_2, and hw2_3. |
+ | * Make the programs executable: | ||
chmod a+rx hw2_* | chmod a+rx hw2_* | ||
− | * | + | * You are now ready to work on all 3 problems for this week, which go in increasing levels of complexity. |
<br /> | <br /> | ||
+ | |||
=Problem 1= | =Problem 1= | ||
<br /> | <br /> | ||
Line 66: | Line 68: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | =A Note on Comparing Binary Files= | ||
+ | <br /> | ||
+ | ==Method 1== | ||
+ | <br /> | ||
+ | Linux has a utility to extract the data section, or the code section, out of an executable file. It is called '''objdump''', and you can use it as illustrated below: | ||
+ | <br /> | ||
+ | * To get the data section: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | |||
+ | objdump -s -j .data hw2_1 | ||
+ | |||
+ | hw2_1: file format elf32-i386 | ||
+ | |||
+ | Contents of section .data: | ||
+ | 80490a4 0a0a5468 65206469 66666572 656e6365 ..The difference | ||
+ | 80490b4 20626574 7765656e 2061206d 61737465 between a maste | ||
+ | 80490c4 7220616e 64206120 62656769 6e6e6572 r and a beginner | ||
+ | 80490d4 0a697320 74686174 20746865 206d6173 .is that the mas | ||
+ | 80490e4 74657220 68617320 6661696c 6564206d ter has failed m | ||
+ | 80490f4 6f726520 6f667465 6e207468 616e2074 ore often than t | ||
+ | 8049104 68652062 6567696e 6e657220 68617320 he beginner has | ||
+ | 8049114 74726965 642e0a2d 2d756e6b 6e6f776e tried..--unknown | ||
+ | 8049124 20736f75 7263650a source. | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | * To get the code, or ".text" section: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | objdump -s -j .text hw2_1 | ||
+ | |||
+ | hw2_1: file format elf32-i386 | ||
+ | |||
+ | Contents of section .text: | ||
+ | 8048080 b8040000 00bb0100 0000b9a4 900408ba ................ | ||
+ | 8048090 77000000 cd80bb00 000000b8 01000000 w............... | ||
+ | 80480a0 cd80 .. | ||
+ | </source> | ||
+ | <br /> | ||
+ | The output is in hex, and the bytes are grouped in blocks of 4 bytes (32 bits), rather than 1 byte at a time, as with '''hexdump.''' But you should be able to recognize ascii and instructions. | ||
+ | <br /> | ||
+ | This way you can compare the text and data sections of your executable to the corresponding sections in the solution executable. | ||
<br /> | <br /> | ||
+ | ''Note: Moodle will use the output of '''objdump''' to compare your executable to the solution program. So only your code and data sections will be compared. Labels at the end of the executable will not be used for comparison.'' | ||
+ | |||
+ | ==Method 2== | ||
<br /> | <br /> | ||
+ | At some point you will have two binary executable files, the one generated from assembling and linking your assembly program, and the copy of my executable. Here's a possible way to check if they are equal: | ||
+ | <br /> | ||
+ | * Move the copy of my executable to a new file with a different name: | ||
+ | |||
+ | mv hw2_1 hw2_1sol | ||
+ | |||
+ | * Assemble and link your program to get a new hw2_1 executable that is yours. | ||
+ | * Compare the two files (your hw2_1 against the new hw2_1sol) using this recipe from [http://superuser.com/questions/125376/how-do-i-compare-binary-files-in-linux superuser.com]: | ||
+ | |||
+ | cmp -l hw2_1 hw2_1sol | gawk '{printf "%08X %02X %02X\n", $1, strtonum(0$2), strtonum(0$3)}' | ||
+ | |||
+ | * The output should be the address in hex where the first difference appears. For example, here's a possible output: | ||
+ | |||
+ | 0000037A 32 34 | ||
+ | |||
+ | :That would indicate that the byte at offset 37A (in hex) in the first file is 32, while it is 34 in the second file. To figure out what's at offset 37A, just '''hexdump''' the files to see the whole contents: | ||
+ | |||
+ | <source lang="text"> | ||
+ | hexdump -v -C hw2_1 | ||
+ | |||
+ | 00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............| | ||
+ | 00000010 02 00 03 00 01 00 00 00 80 80 04 08 34 00 00 00 |............4...| | ||
+ | ... | ||
+ | 00000370 10 00 02 00 00 68 77 32 5f 32 2e 61 73 6d 00 6d |.....hw2_2.asm.m| | ||
+ | 00000380 73 67 31 00 6c 65 6e 31 00 6d 73 67 30 00 6c 65 |sg1.len1.msg0.le| | ||
+ | </source> | ||
<br /> | <br /> | ||
+ | :The difference is on the line 0000370, count 0, 1, 2, ... A and you will find the byte in question. | ||
<br /> | <br /> | ||
+ | |||
+ | |||
+ | <showafterdate after="20151001 12:00" before="20151231 00:00"> | ||
+ | =Solution Programs= | ||
+ | <br /> | ||
+ | ==Program 1== | ||
+ | <source lang="asm"> | ||
+ | ;;; ; hw2_1.asm | ||
+ | ;;; ; D. Thiebaut | ||
+ | ;;; ; | ||
+ | ;;; ; Displays "The difference between a master and a beginner | ||
+ | ;;; ; is that the master has failed more often than the beginner has tried." | ||
+ | ;;; ; | ||
+ | ;;; ; To assemble, link, and run: | ||
+ | ;;; ; nasm -f elf hw2_1.asm | ||
+ | ;;; ; ld -melf_i386 -o hw2_1 hw2_1.o | ||
+ | ;;; ; ./hw2_1 | ||
+ | ;;; ; | ||
+ | |||
+ | section .data | ||
+ | msg1 db 10, 10, "The difference between a master and a beginner", 10 | ||
+ | msg2 db "is that the master has failed more often than the beginner has tried." | ||
+ | msg3 db 10 | ||
+ | len equ $-msg1 | ||
+ | msg4 db "--unknown source", 10 | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | |||
+ | ;;; print message | ||
+ | mov eax, 4 ; write | ||
+ | mov ebx, 1 ; stdout | ||
+ | mov ecx, msg1 | ||
+ | mov edx, len | ||
+ | int 0x80 | ||
+ | |||
+ | ;;; exit | ||
+ | mov ebx, 0 | ||
+ | mov eax, 1 | ||
+ | int 0x80 | ||
+ | |||
+ | </source> | ||
+ | ==Program 2== | ||
+ | <source lang="asm"> | ||
+ | ;;; ; hw2_2.asm | ||
+ | ;;; ; D. Thiebaut | ||
+ | ;;; ; | ||
+ | ;;; ; Displays "Strength is the capacity to break a Hershey bar into four pieces | ||
+ | ;;; ; with your bare hand, and then eat just one of the pieces," followed by two | ||
+ | ;;; ; line-feeds. | ||
+ | ;;; ; | ||
+ | ;;; ; To assemble, link, and run: | ||
+ | ;;; ; nasm -f elf hw2_2.asm | ||
+ | ;;; ; ld -melf_i386 -o hw2_2 hw2_2.o | ||
+ | ;;; ; ./hw2_2 | ||
+ | ;;; ; | ||
+ | |||
+ | section .data | ||
+ | msg1 db "Strength is the capacity to break a Hershey" | ||
+ | len1 equ $-msg1 | ||
+ | msg0 db " bar" | ||
+ | len0 equ $-msg0 | ||
+ | msg2 db " into four pieces with your" | ||
+ | len2 equ $-msg2 | ||
+ | msg3 db "e hands - and then eat " | ||
+ | db "just one of the pieces.",10,10 | ||
+ | len3 equ $-msg3 | ||
+ | |||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | |||
+ | ;;; print message | ||
+ | mov eax, 4 ; write | ||
+ | mov ebx, 1 ; stdout | ||
+ | mov ecx, msg1 | ||
+ | mov edx, len1+len0+len2 | ||
+ | int 0x80 | ||
+ | |||
+ | mov eax, 4 ; write | ||
+ | mov ebx, 1 ; stdout | ||
+ | mov ecx, msg0 | ||
+ | mov edx, len0 | ||
+ | int 0x80 | ||
+ | |||
+ | mov eax, 4 ; write | ||
+ | mov ebx, 1 ; stdout | ||
+ | mov ecx, msg3 | ||
+ | mov edx, len3 | ||
+ | int 0x80 | ||
+ | |||
+ | ;;; exit | ||
+ | mov ebx, 0 | ||
+ | mov eax, 1 | ||
+ | int 0x80 | ||
+ | |||
+ | |||
+ | </source> | ||
+ | ==Program 3== | ||
+ | <source lang="asm"> | ||
+ | ;;; ; hw2_3.asm | ||
+ | ;;; ; D. Thiebaut | ||
+ | ;;; ; | ||
+ | ;;; ; Displays gibberish on the screen. | ||
+ | ;;; ; | ||
+ | ;;; ; To assemble, link, and run: | ||
+ | ;;; ; nasm -f elf hw2_3.asm | ||
+ | ;;; ; ld -melf_i386 -o hw2_3 hw2_3.o | ||
+ | ;;; ; ./hw2_3 | ||
+ | ;;; ; | ||
+ | |||
+ | section .data | ||
+ | msg1 db 7, 9, 10, 10, "asafladfk asafladfk asafladfk " | ||
+ | len4 db $-msg1 | ||
+ | msg3 db 10 | ||
+ | len1 equ $-msg3 | ||
+ | msg4 db "asafladfk ", 7, 9, 9, 7 | ||
+ | len3 equ $-msg4 | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | |||
+ | ;;; print message | ||
+ | mov eax, 4 ; write | ||
+ | mov ebx, 1 ; stdout | ||
+ | mov ecx, msg4 | ||
+ | mov edx, len3 | ||
+ | int 0x80 | ||
+ | |||
+ | mov ebx, 4 | ||
+ | mov eax, ebx ; write | ||
+ | mov ecx, 1 | ||
+ | mov ebx, ecx ; stdout | ||
+ | mov ecx, msg4 | ||
+ | mov edx, len3 | ||
+ | int 0x80 | ||
+ | |||
+ | |||
+ | ;;; exit | ||
+ | mov ebx, 0 | ||
+ | mov ebx, 0 | ||
+ | mov ecx, len3 | ||
+ | mov edx, len3 | ||
+ | mov eax, 1 | ||
+ | mov eax, 1 | ||
+ | int 0x80 | ||
+ | |||
+ | |||
+ | </source> | ||
+ | |||
+ | </showafterdate> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 16:38, 24 September 2015
--D. Thiebaut (talk) 19:57, 23 September 2015 (EDT)
This assignment is due on Wed, Sept. 30, 2015, at 11:55 p.m.
You can work in pair on this assignment. If you do so, make sure you include both names in the header of your program, and
that both students in the pair submit the program on Moodle under their name.
Contents
Preparation
- ssh to aurora with your 231a-xx account.
- get 3 programs from my account on aurora:
wget http://cs.smith.edu/~dthiebaut/handout/hw2_1 wget http://cs.smith.edu/~dthiebaut/handout/hw2_2 wget http://cs.smith.edu/~dthiebaut/handout/hw2_3
- You should now have 3 new files in your directory, called hw2_1, hw2_2, and hw2_3.
- Make the programs executable:
chmod a+rx hw2_*
- You are now ready to work on all 3 problems for this week, which go in increasing levels of complexity.
Problem 1
- Recreate, as exactly as possible, the original program called hw2_1.asm which, when assembled and linked, yielded the program hw2_1.
- Save your program in a file called hw2_1.asm, and make sure that:
- when you assemble and link it (using nasm -f elf, and ld -melf_i386), your executable has the same size as mine, and
- the hexdump of your program matches the hexdump of mine, and, finally,
- the output of your program is the same as the output of mine.
- Submit your program on Moodle, in the HW 2 PB 1 section (when available)
Problem 2
- Recreate, as exactly as possible, the original program called hw2_2.asm which, when assembled and linked, yielded the program hw2_2.
- Save your program in a file called hw2_2.asm, and make sure that:
- when you assemble and link it (using nasm -f elf, and ld -melf_i386), your executable has the same size as mine, and
- the hexdump of your program matches the hexdump of mine, and, finally,
- the output of your program is the same as the output of mine.
- Submit your program on Moodle, in the HW 2 PB 2 section (when available)
Problem 3
- Recreate, as exactly as possible, the original program called hw2_3.asm which, when assembled and linked, yielded the program hw2_3.
- Save your program in a file called hw2_3.asm, and make sure that:
- when you assemble and link it (using nasm -f elf, and ld -melf_i386), your executable has the same size as mine, and
- the hexdump of your program matches the hexdump of mine, and, finally,
- the output of your program is the same as the output of mine.
- Submit your program on Moodle, in the HW 2 PB 3 section (when available)
- Note: I may have used some instructions that we haven't see yet (as of 9/23/15), and which are illustrated in the code below:
mov eax, ebx ; copy the contents of ebx into eax
mov ebx, eax ; copy the contents of eax into ebx
mov ebx, ecx ; copy the contents of ecx into ebx
mov edx, eax ; copy the contents of eax into edx
mov eax, edx ; copy the contents of edx into eax
mov ebx, edx ; copy the contents of edx into ebx
A Note on Comparing Binary Files
Method 1
Linux has a utility to extract the data section, or the code section, out of an executable file. It is called objdump, and you can use it as illustrated below:
- To get the data section:
objdump -s -j .data hw2_1 hw2_1: file format elf32-i386 Contents of section .data: 80490a4 0a0a5468 65206469 66666572 656e6365 ..The difference 80490b4 20626574 7765656e 2061206d 61737465 between a maste 80490c4 7220616e 64206120 62656769 6e6e6572 r and a beginner 80490d4 0a697320 74686174 20746865 206d6173 .is that the mas 80490e4 74657220 68617320 6661696c 6564206d ter has failed m 80490f4 6f726520 6f667465 6e207468 616e2074 ore often than t 8049104 68652062 6567696e 6e657220 68617320 he beginner has 8049114 74726965 642e0a2d 2d756e6b 6e6f776e tried..--unknown 8049124 20736f75 7263650a source.
- To get the code, or ".text" section:
objdump -s -j .text hw2_1 hw2_1: file format elf32-i386 Contents of section .text: 8048080 b8040000 00bb0100 0000b9a4 900408ba ................ 8048090 77000000 cd80bb00 000000b8 01000000 w............... 80480a0 cd80 ..
The output is in hex, and the bytes are grouped in blocks of 4 bytes (32 bits), rather than 1 byte at a time, as with hexdump. But you should be able to recognize ascii and instructions.
This way you can compare the text and data sections of your executable to the corresponding sections in the solution executable.
Note: Moodle will use the output of objdump to compare your executable to the solution program. So only your code and data sections will be compared. Labels at the end of the executable will not be used for comparison.
Method 2
At some point you will have two binary executable files, the one generated from assembling and linking your assembly program, and the copy of my executable. Here's a possible way to check if they are equal:
- Move the copy of my executable to a new file with a different name:
mv hw2_1 hw2_1sol
- Assemble and link your program to get a new hw2_1 executable that is yours.
- Compare the two files (your hw2_1 against the new hw2_1sol) using this recipe from superuser.com:
cmp -l hw2_1 hw2_1sol | gawk '{printf "%08X %02X %02X\n", $1, strtonum(0$2), strtonum(0$3)}'
- The output should be the address in hex where the first difference appears. For example, here's a possible output:
0000037A 32 34
- That would indicate that the byte at offset 37A (in hex) in the first file is 32, while it is 34 in the second file. To figure out what's at offset 37A, just hexdump the files to see the whole contents:
hexdump -v -C hw2_1
00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 02 00 03 00 01 00 00 00 80 80 04 08 34 00 00 00 |............4...|
...
00000370 10 00 02 00 00 68 77 32 5f 32 2e 61 73 6d 00 6d |.....hw2_2.asm.m|
00000380 73 67 31 00 6c 65 6e 31 00 6d 73 67 30 00 6c 65 |sg1.len1.msg0.le|
- The difference is on the line 0000370, count 0, 1, 2, ... A and you will find the byte in question.
<showafterdate after="20151001 12:00" before="20151231 00:00">
Solution Programs
Program 1
;;; ; hw2_1.asm
;;; ; D. Thiebaut
;;; ;
;;; ; Displays "The difference between a master and a beginner
;;; ; is that the master has failed more often than the beginner has tried."
;;; ;
;;; ; To assemble, link, and run:
;;; ; nasm -f elf hw2_1.asm
;;; ; ld -melf_i386 -o hw2_1 hw2_1.o
;;; ; ./hw2_1
;;; ;
section .data
msg1 db 10, 10, "The difference between a master and a beginner", 10
msg2 db "is that the master has failed more often than the beginner has tried."
msg3 db 10
len equ $-msg1
msg4 db "--unknown source", 10
section .text
global _start
_start:
;;; print message
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, msg1
mov edx, len
int 0x80
;;; exit
mov ebx, 0
mov eax, 1
int 0x80
Program 2
;;; ; hw2_2.asm
;;; ; D. Thiebaut
;;; ;
;;; ; Displays "Strength is the capacity to break a Hershey bar into four pieces
;;; ; with your bare hand, and then eat just one of the pieces," followed by two
;;; ; line-feeds.
;;; ;
;;; ; To assemble, link, and run:
;;; ; nasm -f elf hw2_2.asm
;;; ; ld -melf_i386 -o hw2_2 hw2_2.o
;;; ; ./hw2_2
;;; ;
section .data
msg1 db "Strength is the capacity to break a Hershey"
len1 equ $-msg1
msg0 db " bar"
len0 equ $-msg0
msg2 db " into four pieces with your"
len2 equ $-msg2
msg3 db "e hands - and then eat "
db "just one of the pieces.",10,10
len3 equ $-msg3
section .text
global _start
_start:
;;; print message
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, msg1
mov edx, len1+len0+len2
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, msg0
mov edx, len0
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, msg3
mov edx, len3
int 0x80
;;; exit
mov ebx, 0
mov eax, 1
int 0x80
Program 3
;;; ; hw2_3.asm
;;; ; D. Thiebaut
;;; ;
;;; ; Displays gibberish on the screen.
;;; ;
;;; ; To assemble, link, and run:
;;; ; nasm -f elf hw2_3.asm
;;; ; ld -melf_i386 -o hw2_3 hw2_3.o
;;; ; ./hw2_3
;;; ;
section .data
msg1 db 7, 9, 10, 10, "asafladfk asafladfk asafladfk "
len4 db $-msg1
msg3 db 10
len1 equ $-msg3
msg4 db "asafladfk ", 7, 9, 9, 7
len3 equ $-msg4
section .text
global _start
_start:
;;; print message
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, msg4
mov edx, len3
int 0x80
mov ebx, 4
mov eax, ebx ; write
mov ecx, 1
mov ebx, ecx ; stdout
mov ecx, msg4
mov edx, len3
int 0x80
;;; exit
mov ebx, 0
mov ebx, 0
mov ecx, len3
mov edx, len3
mov eax, 1
mov eax, 1
int 0x80
</showafterdate>