Difference between revisions of "CSC231 Homework 1 2017"

From dftwiki3
Jump to: navigation, search
(Problem 2: Programming Assignment)
 
(2 intermediate revisions by the same user not shown)
Line 97: Line 97:
 
<br />
 
<br />
 
<br />
 
<br />
=Problem 2: Programming Assignment=
+
==Problem 2: Programming Assignment==
 
<br />
 
<br />
 
You are given a program that is missing its code section.  Reconstruct the code section without modifying the data section, so that the output of the program is the following:
 
You are given a program that is missing its code section.  Reconstruct the code section without modifying the data section, so that the output of the program is the following:
Line 181: Line 181:
 
<br />
 
<br />
 
<br />
 
<br />
 +
<showafterdate after="20170926 08:00" before="20171231 00:00">
 
<br />
 
<br />
 +
=Solutions=
 
<br />
 
<br />
 +
==Problem 1==
 
<br />
 
<br />
 +
::<source lang="asm">
 +
        section .data
 +
msg1 db "Homework and Chocolate do no mix well!", 10
 +
msgLen equ $-msg1
 +
 +
;;;  ------------------------------------------------------------
 +
;;;  code area
 +
;;;  ------------------------------------------------------------
 +
 +
        section .text
 +
        global  _start
 +
_start:
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, msg1+13
 +
mov edx, 9
 +
int 0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, msg1+msgLen-1
 +
mov edx, 1
 +
int 0x80
 +
;;;  exit()
 +
        mov    eax,1
 +
        mov    ebx,0
 +
        int    0x80
 +
 +
</source>
 +
<br />
 +
=Problem 2=
 +
<br />
 +
::<source lang="asm">
 +
;;; Homework 1 Problem 2 solution program
 +
;;; D. Thiebaut
 +
;;;
 +
section .data
 +
msg            db      "  CSC231 Assembly "
 +
linefeed        db      10
 +
dashline        db      "------------------" ; # 18
 +
lineLen        equ    $-dashline
 +
 +
 +
;; ------------------------------------      # 36
 +
;; --------- CSC231 Assembly ----------      # 9 + 10
 +
;; ------------------------------------
 +
                section .text
 +
                global  _start
 +
_start:
 +
;;; First Line
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, dashline ; print dashline
 +
mov edx, lineLen
 +
int 0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, dashline ; print anothe line of dashes
 +
mov edx, lineLen  ; on the same line
 +
int 0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, linefeed ; go to the next line
 +
mov edx, 1
 +
int 0x80
 +
 +
;;; Second Line
 +
 +
                mov    eax, 4
 +
mov    ebx, 1
 +
mov    ecx, dashline ; print only 9 dashes
 +
mov    edx, 9
 +
int    0x80
 +
 +
                mov    eax, 4
 +
mov    ebx, 1
 +
mov    ecx, msg+1 ; print the message,
 +
mov    edx, lineLen-1 ; but starting at the 2nd
 +
int    0x80 ; space.
 +
 +
                mov    eax, 4
 +
mov    ebx, 1
 +
mov    ecx, dashline  ; print 10 dashes
 +
mov    edx, 10
 +
int    0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, linefeed ; go to the next line
 +
mov edx, 1
 +
int 0x80
 +
 +
;;; Third Line
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, dashline ; print dash line
 +
mov edx, lineLen
 +
int 0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, dashline ; print dash line (stay on same line)
 +
mov edx, lineLen
 +
int 0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, linefeed ; go to next line
 +
mov edx, 1
 +
int 0x80
 +
 +
 +
;;; exit and return to Linux prompt
 +
 +
                mov    ebx, 0
 +
                mov    eax, 1
 +
                int    0x80
 +
</source>
 +
<br />
 +
==Problem 3==
 +
<br />
 +
Just run the commands in a Linux Terminal/Windows and you will figure out how to do it! :-)
 +
</showafterdate>
 +
<br />
 +
<onlydft>
 +
=VPL=
 +
==vpl_run.sh==
 +
<source lang="bash">
 +
#! /bin/bash
 +
# D. Thiebaut
 +
# Smith College
 +
# vpl_evaluate.sh script looping through several tests, each test with
 +
# its own input file and its own expected output file.  The output of the
 +
# student program is tested against the expected output file.
 +
# set -x
 +
 +
 +
cat > vpl_execution <<EEOOFF
 +
#! /bin/bash
 +
 +
# --- program tested (no extension) ---
 +
prog1=hw1
 +
prog1sol=hw1sol
 +
 +
# --- compile solution program
 +
nasm -f elf \${prog1sol}.asm
 +
ld -melf_i386 \${prog1sol}.o -o \$prog1sol
 +
 +
# --- compile student program ---
 +
if  nasm -f elf \${prog1}.asm ; then
 +
    echo "nasm pass: Ok!"
 +
else
 +
    #echo "Syntax error found in asm file"
 +
    grade=50
 +
    #echo "Grade :=>> \$grade"
 +
    exit 0
 +
fi
 +
 +
if  ld -melf_i386 \${prog1}.o -o \$prog1 ; then
 +
    echo "ld pass: Ok!"
 +
else
 +
    #echo "Comment :=>> Linker error found in object file(s)"
 +
    grade=50
 +
    #echo "Grade :=>> \$grade"
 +
    exit 0
 +
fi
 +
 +
./\${prog1}
 +
 +
EEOOFF
 +
 +
 +
chmod +x vpl_execution
 +
</source>
 +
==vpl_evaluate.sh==
 +
<source lang="bash">
 +
#! /bin/bash
 +
# D. Thiebaut
 +
# Smith College
 +
# vpl_evaluate.sh script looping through several tests, each test with
 +
# its own input file and its own expected output file.  The output of the
 +
# student program is tested against the expected output file.
 +
# set -x
 +
 +
 +
cat > vpl_execution <<EEOOFF
 +
#! /bin/bash
 +
 +
# --- program tested (no extension) ---
 +
prog1=hw1
 +
prog1sol=hw1sol
 +
 +
# --- compile solution program
 +
nasm -f elf \${prog1sol}.asm
 +
ld -melf_i386 \${prog1sol}.o -o \$prog1sol
 +
 +
# --- compile student program ---
 +
if  nasm -f elf \${prog1}.asm ; then
 +
    echo "Comment :=>> nasm pass Ok!"
 +
else
 +
    echo "Comment :=>> Syntax error found in asm file"
 +
    grade=50
 +
    echo "Grade :=>> \$grade"
 +
    exit 0
 +
fi
 +
 +
if  ld -melf_i386 \${prog1}.o -o \$prog1 ; then
 +
    echo "Comment :=>> ld pass Ok!"
 +
else
 +
    echo "Comment :=>> Linker error found in object file(s)"
 +
    grade=50
 +
    echo "Grade :=>> \$grade"
 +
    exit 0
 +
fi
 +
 +
# --- compute data size ---
 +
userDataSize=\`size \${prog1} | tail -n -1 | cut -f 2 | tr -s " "\`
 +
 +
#--- create expected outputs, one for each input file above ---
 +
./\${prog1sol}  &> data1.out
 +
data1_len=\$(stat -c%s "data1.out")
 +
 +
# default grade
 +
grade=60
 +
 +
 +
echo "Comment :=>>-TEST DATA SEGMENT"
 +
  # ==============================================
 +
  # TEST DATA SEGMENT
 +
  # ==============================================
 +
  #--- get data segment of student program ---
 +
  readelf -x .data \${prog1} &> user.data
 +
  cat user.data | tail -n 4 | head -n 3 |  sed 's/^ *//' | cut -d" " -f2- > dummy.data
 +
  mv dummy.data user.data
 +
  #echo "user.data"
 +
  #cat user.data
 +
 +
  #--- get data segment of solution program ---
 +
  readelf -x .data \${prog1sol} &> solution.data
 +
  cat solution.data | tail -n 4 | head -n 3 |  sed 's/^ *//' | cut -d" " -f2- > dummy.data
 +
  mv dummy.data solution.data
 +
  #echo "solution data"
 +
  #cat solution.data
 +
 +
  #--- compute difference between outputs --- 
 +
  diff -y -w --ignore-all-space user.data solution.data > diff.out
 +
 
 +
  #--- reject if different ---
 +
  if ((\$? > 0)); then   
 +
      echo "Comment :=>> It seems that you have modified the original data section"
 +
      echo "Comment :=>> The data segment of your program does not match the original"
 +
      echo "Comment :=>> data segment given in the homework assignment."
 +
      grade=75  # C
 +
      echo "Grade :=>> \$grade"
 +
      exit 0 
 +
  else
 +
      echo "Comment :=>> The data segment of your program is correct.  Ok!"
 +
  fi
 +
 +
echo "Comment :=>>-TEST PROGRAM OUTPUT "
 +
  # ==============================================
 +
  # TEST OUTPUT
 +
  # ==============================================
 +
  #--- run program, capture output ---
 +
  ./\${prog1}  &> user.out
 +
  user_len=\$(stat -c%s "user.out")
 +
 +
  #--- compute difference between outputs --- 
 +
  diff -y -w --ignore-all-space user.out data1.out > diff.out
 +
 
 +
  #--- reject if different ---
 +
  if ((\$? > 0)); then
 +
     
 +
      echo "Comment :=>> The output of your program is not the same as that of the solution program."
 +
      echo "Comment :=>> Your program outputs \$user_len characters, while the solution program"
 +
      echo "Comment :=>> outputs \$data1_len characters (including linefeeds)"
 +
      echo "Comment :=>> ---------------"
 +
      echo "Comment :=>> Your output:"
 +
      echo "Comment :=>> ---------------"
 +
      echo "<|--"
 +
      cat user.out
 +
      echo "--|>"
 +
      echo ""
 +
      echo "Comment :=>> ---------------"
 +
      echo "Comment :=>> Expected output: "
 +
      echo "Comment :=>> ---------------"
 +
      echo "<|--"
 +
      cat data1.out
 +
      echo "--|>"
 +
      grade=75 # C
 +
      echo "Grade :=>> \$grade"
 +
      exit 0
 +
      # --------------------- REWARD IF CORRECT OUTPUT -----------------
 +
  else
 +
      grade=100 # B
 +
      echo "Comment :=>>- The output of your program is correct!"
 +
  fi
 +
 +
 +
if (( grade > 96 )); then
 +
  grade=100
 +
fi
 +
 +
echo "Grade :=>> \$grade"
 +
 +
EEOOFF
 +
 +
 +
chmod +x vpl_execution
 +
 +
</source>
 +
==hw1sol.asm==
 +
<source lang="asm">
 +
section .data
 +
msg            db      "  CSC231 Assembly "
 +
linefeed        db      10
 +
dashline        db      "------------------" ; # 18
 +
lineLen        equ    $-dashline
 +
 +
 +
;; ------------------------------------      # 36
 +
;; --------- CSC231 Assembly ----------      # 9 + 10
 +
;; ------------------------------------
 +
                section .text
 +
                global  _start
 +
_start:
 +
;;; First Line
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, dashline
 +
mov edx, lineLen
 +
int 0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, dashline
 +
mov edx, lineLen
 +
int 0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, linefeed
 +
mov edx, 1
 +
int 0x80
 +
 +
;;; Second Line
 +
 +
                mov    eax, 4
 +
mov    ebx, 1
 +
mov    ecx, dashline
 +
mov    edx, 9
 +
int    0x80
 +
 +
                mov    eax, 4
 +
mov    ebx, 1
 +
mov    ecx, msg+1
 +
mov    edx, lineLen-1
 +
int    0x80
 +
 +
                mov    eax, 4
 +
mov    ebx, 1
 +
mov    ecx, dashline
 +
mov    edx, 10
 +
int    0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, linefeed
 +
mov edx, 1
 +
int 0x80
 +
 +
;;; Third Line
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, dashline
 +
mov edx, lineLen
 +
int 0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, dashline
 +
mov edx, lineLen
 +
int 0x80
 +
 +
mov eax, 4
 +
mov ebx, 1
 +
mov ecx, linefeed
 +
mov edx, 1
 +
int 0x80
 +
 +
 +
;;; exit.  Do not add code below this line.                                                                                                               
 +
                mov    ebx, 0
 +
                mov    eax, 1
 +
                int    0x80
 +
               
 +
               
 +
</source>
 +
</onlydft>
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 13:35, 25 September 2017

--D. Thiebaut (talk) 17:42, 16 September 2017 (EDT)




This assignment is due on Monday Sept. 25, at 11:55 p.m. You can work in pairs on this assignment, in which case you should include both names in the header of the assembly program you will submit for Problem 2.




Problem #1


Connect to your 231 account, and create this short assembly program, which you can call play1.asm:

;;; ; play1.asm
;;; ; program for Homework 1
;;; ; -------------------------------------------------------------------

;;;  ------------------------------------------------------------
;;;  data areas
;;;  ------------------------------------------------------------

	        section .data
msg1		db	"Homework 1", 10
msgLen		equ	$-msg1
	
;;;  ------------------------------------------------------------
;;;  code area
;;;  ------------------------------------------------------------

	        section .text
	        global  _start
_start:
		mov	eax, 4
		mov	ebx, 1
		mov	ecx, msg1
		mov	edx, msgLen
		int	0x80
;;;  exit()
	        mov     eax,1
	        mov     ebx,0
	        int     0x80	; final system call
  • Assemble, link and run the program. Verify that prints what it should be printing.
  • Generate a listing file, by using the -l (minus ell)
nasm -f elf -l play1.lst play1.asm

  • Edit the play1.lst file with emacs, and observe the relationship between the opcodes on the left and the corresponding instructions on the right.
  • Go back to the play1.asm and change the mov ecx, msg1 line to mov ecx, msg1+4, and change the mov edx, msgLen to mov edx, 5.
  • Assemble, link and run the program. Notice that the output is now different.
  • Generate a new listing file for the new play1.asm, and see how the opcodes of the two instructions you changed are now different. Make sure you understand how the opcodes relate to the instructions.


Your Assignment


Recreate the assembly program whose opcodes are shown below. I have removed the actual assembly part to leave only the opcodes.

     1                               
     2 00000000 486F6D65776F726B20-
     3 00000009 616E642043686F636F-
     4 00000012 6C61746520646F206E-
     5 0000001B 6F206D69782077656C-
     6 00000024 6C210A             
     7 
     8                                  	
     9 
    10 
    11 
    12                                  
    13 
    14 
    15 
    16 00000000 B804000000 
    17 00000005 BB01000000 
    18 0000000A B9[0D000000]
    19 0000000F BA09000000  
    20 00000014 CD80        
    21                      
    22 00000016 B804000000 
    23 0000001B BB01000000 
    24 00000020 B9[26000000]
    25 00000025 BA01000000  
    26 0000002A CD80        
    27                      
    28 0000002C B801000000  
    29 00000031 BB00000000  
    30 00000036 CD80


Question
What is the output of this mystery program? Please answer this question on Moodle. You do not need to submit the program you created. You only need to figure out what it prints.



Problem 2: Programming Assignment


You are given a program that is missing its code section. Reconstruct the code section without modifying the data section, so that the output of the program is the following:

cs231a-xx@aurora ~ $ ./hw1
------------------------------------
---------  CSC231 Assembly ----------
------------------------------------
cs231a-xx@aurora ~ $

The incomplete source code of your program is given below:

;;; hw1.asm
;;; Your name
;;;  
;;; Add a description here
;;;              
;;; To assemble, link, and run:  
;;;     nasm -f elf  hw1.asm
;;;     ld -melf_i386 -o hw1 hw1.o
;;;     ./hw1
;;;      
                section .data
;;; you are not allowed to make any changes to the data section
;;; below.
msg             db      "  CSC231 Assembly "
linefeed	db	10
dashline        db	"------------------"
lineLen		equ	$-dashline


		section .text
                global  _start
_start:
;;; put your code below this point

;;; exit.  Do not add code below this line.                                                                                                                 
                mov     ebx, 0
                mov     eax, 1
                int     0x80


Call your program hw1.asm and submit it to the programming section of Homework 1 on Moodle when you are done.

Make sure the output of your program matches the output shown above, EXACTLY!

Also, make sure you do not make any changes to the data section: this means no new string, no new labels, no new constants! The autograder on Moodle will compare the data section of your program to the that of the solution program, and if they are different, you will not be given credit for your program, even if it outputs the correct strings!

Documentation


Document your code well. Use programs distributed or made available on the class schedule page as examples of what you should emulate. You do not need to document each instruction, but you should document each group of instructions with a header indicating what the block's function is.

Submission


Submit your program on Moodle. Exceptionally, you will be allowed to evaluate your program with the auto-grader, a feature that will not be available in the future. This will give you a sense of how the auto-grader parses and evaluates your code.

Problem 3


Assume that you connect to your cs231a-xx account and type the following commands:

cd
mkdir temp1
mkdir temp1/temp2
cd temp1/temp2
mkdir temp3
mkdir temp4
cd temp4
mkdir temp5
mkdir temp5/temp6
cd temp5
cd temp6
emacs myprog.asm 
cd


Go to Moodle, and locate this problem as the "Bash Problem", and in the series of Linux commands listed there, identify the one group that would correctly display the contents of the myprog.asm file?


<showafterdate after="20170926 08:00" before="20171231 00:00">

Solutions


Problem 1


	        section .data
msg1		db	"Homework and Chocolate do no mix well!", 10
msgLen		equ	$-msg1
	
;;;  ------------------------------------------------------------
;;;  code area
;;;  ------------------------------------------------------------

	        section .text
	        global  _start
_start:
		mov	eax, 4
		mov	ebx, 1
		mov	ecx, msg1+13
		mov	edx, 9
		int	0x80
	
		mov	eax, 4
		mov	ebx, 1
		mov	ecx, msg1+msgLen-1
		mov	edx, 1
		int	0x80	
;;;  exit()
	        mov     eax,1
	        mov     ebx,0
	        int     0x80


Problem 2


;;; Homework 1 Problem 2 solution program
;;; D. Thiebaut
;;; 
		section	.data
msg             db      "  CSC231 Assembly "
linefeed        db      10
dashline        db      "------------------" ; # 18
lineLen         equ     $-dashline


;; ------------------------------------      # 36
;; --------- CSC231 Assembly ----------      # 9 + 10
;; ------------------------------------
                section .text
                global  _start
_start:
;;; First Line
		mov	eax, 4
		mov	ebx, 1
		mov	ecx, dashline ; print dashline
		mov	edx, lineLen
		int	0x80
	
		mov	eax, 4
		mov	ebx, 1
		mov	ecx, dashline ; print anothe line of dashes
		mov	edx, lineLen  ; on the same line
		int	0x80	

		mov	eax, 4
		mov	ebx, 1
		mov	ecx, linefeed ; go to the next line
		mov	edx, 1
		int	0x80

;;; Second Line
	
                mov     eax, 4
		mov     ebx, 1
		mov     ecx, dashline ; print only 9 dashes
		mov     edx, 9
		int     0x80
	
                mov     eax, 4
		mov     ebx, 1
		mov     ecx, msg+1 	; print the message, 
		mov     edx, lineLen-1	; but starting at the 2nd
		int     0x80		; space.

                mov     eax, 4
		mov     ebx, 1
		mov     ecx, dashline   ; print 10 dashes
		mov     edx, 10
		int     0x80

		mov	eax, 4
		mov	ebx, 1
		mov	ecx, linefeed 	; go to the next line
		mov	edx, 1
		int	0x80

;;; Third Line
		mov	eax, 4
		mov	ebx, 1
		mov	ecx, dashline ; print dash line
		mov	edx, lineLen
		int	0x80
	
		mov	eax, 4
		mov	ebx, 1
		mov	ecx, dashline ; print dash line (stay on same line)
		mov	edx, lineLen
		int	0x80	

		mov	eax, 4
		mov	ebx, 1
		mov	ecx, linefeed ; go to next line
		mov	edx, 1
		int	0x80

	
;;; exit and return to Linux prompt
	
                mov     ebx, 0
                mov     eax, 1
                int     0x80


Problem 3


Just run the commands in a Linux Terminal/Windows and you will figure out how to do it! :-) </showafterdate>


...