CSC231 Homework 4 Solutions 2014
--D. Thiebaut (talk) 16:16, 21 October 2014 (EDT)
Contents
Problem #1
<br />
;;; ; -------------------------------------------------------------------
;;; ; hw4a.asm
;;; ; Lei Lei
;;; ;
;;; ; Given the following definition:
;;; ; msg db 3,"ALL",11,"PROGRAMMERS",3,"ARE",11,"PLAYWRIGHTS"
;;; ; db 3,"AND",3,"ALL",9,"COMPUTERS",3,"ARE",5,"LOUSY"
;;; ; db 6,"ACTORS"
;;; ; This program outputs each letter of msg capitalized in a line:
;;; ;
;;; ; All
;;; ; Programmers
;;; ; Are
;;; ; Playwrights
;;; ; And
;;; ; All
;;; ; Computers
;;; ; Are
;;; ; Lousy
;;; ; Actors
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ; nasm -f elf -F stabs hw4a.asm
;;; ; ld -o hw4a hw4a.o
;;; ; ./hw4a
;;; ; -------------------------------------------------------------------
;;; ; -----------------------------------------------------------
;;; ; Constants
;;; ; -----------------------------------------------------------
EXIT equ 1
WRITE equ 4
STDOUT equ 1
;;; ------------------------------------------------------------
;;; data areas
;;; ------------------------------------------------------------
section .data
msg db 3,"ALL",11,"PROGRAMMERS",3,"ARE",11,"PLAYWRIGHTS"
db 3,"AND",3,"ALL",9,"COMPUTERS",3,"ARE",5,"LOUSY"
db 6,"ACTORS"
N equ 10
newLine db 0x0A
line_len equ 1
counter dd 0
temp1 dd 0
temp2 dd 0
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global _start
_start: mov ecx, N
for: mov dword[temp1], ecx ;Main loop, loop for N=10 times
mov esi, msg
add esi, dword[counter] ;esi points to integer
inc dword[counter]
mov eax, WRITE
mov ebx, STDOUT
mov ecx, msg
add ecx, dword[counter] ;ecx points to first letter
mov edx, 1
int 0x80 ;print first letter in cap
movzx ecx, byte[byte esi] ;move the integer to ecx
dec ecx ;minus the already printed first letter
inc dword[counter]
call printLower
call printLine
mov ecx, dword[temp1]
loop for
;;; exit()
mov eax,EXIT
mov ebx,0
int 0x80 ; final system call
;;; ----------------------------------------------------------------
;;; printLower: Print letters in lower case
;;; ----------------------------------------------------------------
printLower:
forLower:
mov dword[temp2], ecx
mov eax, WRITE
mov ebx, STDOUT
mov ecx, msg
add ecx, dword[counter]
add dword[ecx], 'a'-'A' ;change into lower case
mov edx, 1
int 0x80
inc dword[counter]
mov ecx, dword[temp2]
loop forLower
ret
;;; ---------------------------------------------------------------
;;; printLine: Hard break and print new line
;;; ---------------------------------------------------------------
printLine:
section .bss
.temp resd 1
section .text
mov eax, WRITE
mov ebx, STDOUT
mov edx, line_len
mov dword[.temp], ecx
lea ecx, [newLine]
int 0x80
mov ecx, dword[.temp]
ret