Difference between revisions of "CSC231 Homework 2 Solutions 2012"
(→Problem #2) |
|||
Line 151: | Line 151: | ||
</source> | </source> | ||
− | However, Szilvi used instructions we haven't covered yet. The program that is the next smallest, using the constructs we have seen so far is due to Sydney. | + | However, Szilvi used instructions we haven't covered yet. The program that is the next smallest, using the constructs we have seen so far is due to Sydney. Its executable contains 1035 bytes. |
<br /> | <br /> |
Latest revision as of 09:17, 24 September 2012
--D. Thiebaut 09:58, 24 September 2012 (EDT)
Problem #1
;;; hw2a.asm
;;; D. Thiebaut
;;;
;;; Display "Hello there!" on the screen
;;;
;;; To assemble, link, and run:
;;; nasm -f elf -F stabs hw2a.asm
;;; ld -melf_i386 -o hw2a hw2a.o
;;; ./hw2a
;;;
section .data
I db "I "
Ilen equ $-I
hate db "hate "
chocolate db "chocolate, "
chocoLen equ $-chocolate
db "and prefer "
anchovies db "anchovies, "
anchoviesLen equ $-anchovies-1 ; -1 to skip comma
db "and I especially "
like db "like "
likeLen equ $-like
db "them "
whipped db "with whipped cream on top!", 10, 10
whippedLen equ $-whipped
section .text
global _start
_start:
;;; print message
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, I ; I
mov edx, 2
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, like ; like
mov edx, likeLen
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, chocolate+1
mov edx, 2 ; "ho"
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, chocolate+7
mov edx, 1 ; "t"
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, chocolate-1; " chocolate" (with a leading space)
mov edx, chocoLen+1
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, whipped ; with whipped cream on top
mov edx, whippedLen
int 0x80
;;; exit
mov ebx, 0
mov eax, 1
int 0x80
To grade this homework I generated the hexdump for my original program and the hexdump for your programs and compared the two with the command diff -wy -W 172 hw2asol.hexdump hw2a.hexdump.
This generated a wide listing with the two hexdumps side by side, and a vertical bar in the middle of the page when two lines were different. This allowed me to quickly identify discrepancies.
000000e0 00 00 b9 16 91 04 08 ba 0c 00 00 00 cd 80 b8 04 |................| 000000e0 00 00 b9 16 91 04 08 ba 0c 00 00 00 cd 80 b8 04 |................|
000000f0 00 00 00 bb 01 00 00 00 b9 53 91 04 08 ba 1c 00 |.........S......| 000000f0 00 00 00 bb 01 00 00 00 b9 53 91 04 08 ba 1c 00 |.........S......|
00000100 00 00 cd 80 bb 00 00 00 00 b8 01 00 00 00 cd 80 |................| | 00000100 00 00 cd 80 b8 01 00 00 00 bb 00 00 00 00 cd 80 |................|
00000110 49 20 68 61 74 65 20 63 68 6f 63 6f 6c 61 74 65 |I hate chocolate| 00000110 49 20 68 61 74 65 20 63 68 6f 63 6f 6c 61 74 65 |I hate chocolate|
00000120 2c 20 61 6e 64 20 70 72 65 66 65 72 20 61 6e 63 |, and prefer anc| 00000120 2c 20 61 6e 64 20 70 72 65 66 65 72 20 61 6e 63 |, and prefer anc|
In the example above, notice that the b8 and bb bytes are different in the two hexdumps...
Problem #2
The shortest program is due this week to Szilvi, with 1033 bytes:
;;; hw2b.asm
;;; Szilvi Kiss
;;; 231a-aj
;;; September 15, 2012
;;;
;;;
;;; Displays 6 rows of stars on the screen.
;;;
;;; To assemble, link, and run:
;;; nasm -f elf -F stabs hw2b.asm
;;; ld -melf_i386 -o hw2b hw2b.o
;;; ./hw2b
;;;
section .data
stars db "*********************",10
starsLen equ $-stars
section .text
global _start
;;; start
_start:
mov edi, 0 ;; initialize counter
;;; loop
loop:
add edi, 1 ;; increment counter
;; print a line of stars
mov eax, 4
mov ebx, 1
mov ecx, stars
mov edx, starsLen
int 0x80
;; check number of lines printed so far
cmp edi, 6
;; if number of lines is NOT equal to 6, loop again
jne loop
;;; exit
mov eax, 1
mov ebx, 0
int 0x80
However, Szilvi used instructions we haven't covered yet. The program that is the next smallest, using the constructs we have seen so far is due to Sydney. Its executable contains 1035 bytes.
;;; Sydney Ness
;;; September 19
;;; hw2b.asm
;;;
;;; A program that outputs six lines of stars with a
;;; minimally sized executable.
;;;
;;; size of executable in bytes: 1035
;;;
;;;
section .data
stars db "*********************", 10
db "*********************", 10
db "*********************", 10
db "*********************", 10
db "*********************", 10
db "*********************", 10
section .text
global _start
_start:
;;; print stars
mov eax, 4
mov ebx, 1
mov ecx, stars
mov edx, 132
int 0x80
;;; ; exit
mov ebx, 0
mov eax, 1
int 0x80
Problem #3
;;; Sydney Ness
;;; 231a-ag
;;; hw2c.asm
;;; 19 September
;;;
;;; prints this string (between two linux
;;; prompts) on the screen:
;;; [231a-ag@beowulf ~/hw2]$ ./hw2c
;;; I hate quotations. Tell me what you know.
;;;
;;; Ralph Waldo Emerson (1803 - 1882)
;;;
;;; [231a-ag@beowulf ~/hw2]$
;;;
section .data
msg2 db "Tell me what you know."
msg2Len equ $-msg2
msg3 db " Ralph Waldo Emerson "
msg3Len equ $-msg3
msg5 db 10, 10
msg5Len equ $-msg5
msg1 db "I hate quotations. "
msg1Len equ $-msg1
msg4 db "(1803 - 1882)"
msg4Len equ $-msg4
section .text
global _start
_start:
;;; print "I hate quotations. "
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, msg1Len
int 0x80
;;; print "Tell me what you know."
mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, msg2Len
int 0x80
;;; print 2 line-feed chars
mov eax, 4
mov ebx, 1
mov ecx, msg5
mov edx, msg5Len
int 0x80
;;; print " Ralph Waldo Emerson "
mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, msg3Len
int 0x80
;;; print "(1803-1882)"
mov eax, 4
mov ebx, 1
mov ecx, msg4
mov edx, msg4Len
int 0x80
;;; print 2 line-feed characters
mov eax, 4
mov ebx, 1
mov ecx, msg5
mov edx, msg5Len
int 0x80
;;; exit
mov ebx, 0
mov eax, 1
int 0x80