CSC231 Homework 1 Solutions 2010
--D. Thiebaut 13:52, 28 September 2010 (UTC)
hw1a.asm
;;; -------------------------------------------------------------------
;;; hw1a.asm
;;;
;;; RB Axtell
;;; CSC 231
;;; 18/9/2010
;;;
;;; Size of executable: 1306 bytes measured with the command
;;;
;;; echo `ls -l hw1a | cut -d' ' -f 5` bytes
;;;
;;;
;;; Homework 1, Problem 1 :
;;;
;;; Prints :
;;;
;;; abcdefghiklmnopqrstuvwxyz
;;; abcdefghiklmnopqrstuvw
;;; abcdefghiklmnopqrst
;;; abcdefghiklmnopq
;;; abcdefghiklmn
;;; abcdefghik
;;; abcdefg
;;; abcd
;;; a
;;;
;;;
;;; to assemble and run:
;;;
;;; nasm -f elf -F stabs hw1a.asm
;;; ld -melf_i386 -o hw1a hw1a.o
;;; ./hw1a
;;;
;;; -------------------------------------------------------------------
;%include files here...
EXIT equ 1
WRITE equ 4
STDOUT equ 1
;; ------------------------------------------------------------
;; data area
;; ------------------------------------------------------------
section .data
msg db "abcdefghijklmnopqrstuzwxyz",10
db "abcdefghijklmnopqrstuzw",10
db "abcdefghijklmnopqrst",10
db "abcdefghijklmnopq",10
db "abcdefghijklmn",10
db "abcdefghijk",10
db "abcdefgh",10
db "abcde",10
db "a",10
MSGLEN equ $-msg
;; ------------------------------------------------------------
;; code area
;; ------------------------------------------------------------
section .text
global _start
_start: mov eax, WRITE ; all nine lines
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN
int 0x80
;; exit()
mov eax,EXIT
mov ebx,0
int 0x80 ; final system call
hw1b.asm
;;; -------------------------------------------------------------------
;;; hw1b.asm
;;;
;;; RB Axtell
;;; CSC 231
;;; 18/9/2010
;;;
;;; Size of executable: 3632 bytes measured with the command
;;;
;;; echo `ls -l hw1b | cut -d' ' -f 5` bytes
;;;
;;; The B version of the program is almost three times as large as
;;; the A version. B uses 18 blocks to print; 1 for each line and one
;;; for each line break while A only uses 1. B doesn't have as much in
;;; the data section as A, but it takes so many more calls to print it
;;; all out,that it isn't worth the space saved in the data section.
;;;
;;; Homework 1, Problem 2 :
;;;
;;; Prints :
;;;
;;; abcdefghiklmnopqrstuvwxyz
;;; abcdefghiklmnopqrstuvw
;;; abcdefghiklmnopqrst
;;; abcdefghiklmnopq
;;; abcdefghiklmn
;;; abcdefghik
;;; abcdefg
;;; abcd
;;; a
;;;
;;;
;;; to assemble and run:
;;;
;;; nasm -f elf -F stabs hw1b.asm
;;; ld -melf_i386 -o hw1b hw1b.o
;;; ./hw1b
;;;
;;; -------------------------------------------------------------------
;%include files here...
EXIT equ 1
WRITE equ 4
STDOUT equ 1
;; ------------------------------------------------------------
;; data area
;; ------------------------------------------------------------
section .data
msg db "abcdefghijklmnopqrstuvwxyz"
MSGLEN equ $-msg
newln db 10
;; ------------------------------------------------------------
;; code area
;; ------------------------------------------------------------
section .text
global _start
_start: mov eax, WRITE ; print abcdefghiklmnopqrstuvwxyz
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN
int 0x80
mov eax, WRITE ; line break
mov ebx, STDOUT
mov ecx, newln
mov edx, 1
int 0x80
mov eax, WRITE ; abcdefghiklmnopqrstuvw
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN-3 ; length of alphabet minus 3
int 0x80
mov eax, WRITE ; line break
mov ebx, STDOUT
mov ecx, newln
mov edx, 1
int 0x80
mov eax, WRITE ; abcdefghiklmnopqrst
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN-6 ; length of alphabet minus 6
int 0x80
mov eax, WRITE ; line break
mov ebx, STDOUT
mov ecx, newln
mov edx, 1
int 0x80
mov eax, WRITE ; abcdefghiklmnopq
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN-9
int 0x80
mov eax, WRITE ; line break
mov ebx, STDOUT
mov ecx, newln
mov edx, 1
int 0x80
mov eax, WRITE ; abcdefghiklmn
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN-12
int 0x80
mov eax, WRITE ; line break
mov ebx, STDOUT
mov ecx, newln
mov edx, 1
int 0x80
mov eax, WRITE ; abcdefghik
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN-15
int 0x80
mov eax, WRITE ; line break
mov ebx, STDOUT
mov ecx, newln
mov edx, 1
int 0x80
mov eax, WRITE ; abcdefg
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN-18
int 0x80
mov eax, WRITE ; line break
mov ebx, STDOUT
mov ecx, newln
mov edx, 1
int 0x80
mov eax, WRITE ; abcd
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN-21
int 0x80
mov eax, WRITE ; line break
mov ebx, STDOUT
mov ecx, newln
mov edx, 1
int 0x80
mov eax, WRITE ; print "a", the final line
mov ebx, STDOUT
mov ecx, msg
mov edx, MSGLEN-25
int 0x80
mov eax, WRITE ; line break
mov ebx, STDOUT
mov ecx, newln
mov edx, 1
int 0x80
;; exit()
mov eax,EXIT
mov ebx,0
int 0x80 ; final system call