Difference between revisions of "CSC231 Homework 2 2015"

From dftwiki3
Jump to: navigation, search
(Problem 3)
Line 66: Line 66:
 
</source>
 
</source>
 
<br />
 
<br />
 +
=A Note on Comparing Binary Files=
 
<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 />
 
<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 different 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 />
 
<br />
 
<br />

Revision as of 19:27, 23 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.



Preparation


  • ssh to aurora with your 231a-xx account.
  • get 3 programs from my 231a 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 

  • 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


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