CSC231 Homework 2 2015

From dftwiki3
Revision as of 14:54, 24 September 2015 by Thiebaut (talk | contribs)
Jump to: navigation, search

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


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>