Difference between revisions of "CSC231 Homework 4 Solutions 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <br /> __TOC__ <br /> =Problem #1= <br /> <source lang="asm"> <br /> ;;; ; ------------------------------------------------------------------- ;;; ; hw4a.asm ;;; ;...")
 
Line 5: Line 5:
 
<br />
 
<br />
 
=Problem #1=
 
=Problem #1=
 +
<br />
 
<br />
 
<br />
 
<source lang="asm">
 
<source lang="asm">
<br />
+
 
 
;;; ; -------------------------------------------------------------------
 
;;; ; -------------------------------------------------------------------
 
;;; ; hw4a.asm
 
;;; ; hw4a.asm
Line 71: Line 72:
 
_start: mov ecx, N
 
_start: mov ecx, N
 
 
for: mov dword[temp1], ecx ;Main loop, loop for N=10 times
+
for: push ecx ;Main loop, loop for N=10 times
 
mov esi, msg
 
mov esi, msg
 
  add esi, dword[counter] ;esi points to integer
 
  add esi, dword[counter] ;esi points to integer
Line 90: Line 91:
 
  call printLine
 
  call printLine
  
  mov ecx, dword[temp1]
+
  pop ecx
 
  loop for
 
  loop for
 
 
Line 104: Line 105:
 
printLower:
 
printLower:
 
forLower:
 
forLower:
mov dword[temp2], ecx
+
        push ecx
 +
 
 
mov eax, WRITE
 
mov eax, WRITE
 
mov ebx, STDOUT
 
mov ebx, STDOUT
Line 115: Line 117:
  
 
inc dword[counter]
 
inc dword[counter]
mov ecx, dword[temp2]
+
pop  ecx
 
loop forLower
 
loop forLower
 
ret
 
ret
Line 130: Line 132:
 
mov ebx, STDOUT
 
mov ebx, STDOUT
 
mov edx, line_len
 
mov edx, line_len
mov dword[.temp], ecx
+
push  ecx
 
lea ecx, [newLine]
 
lea ecx, [newLine]
 
int 0x80
 
int 0x80
mov ecx, dword[.temp]
+
pop ecx
 
ret
 
ret
 
 

Revision as of 15:17, 21 October 2014

--D. Thiebaut (talk) 16:16, 21 October 2014 (EDT)



Contents


Problem #1



;;; ; -------------------------------------------------------------------
;;; ; 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:	push 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

 	pop	ecx
 	loop	for
	
;;;  exit()

	mov	eax,EXIT
	mov	ebx,0
	int	0x80			; final system call

;;; ----------------------------------------------------------------
;;; printLower: Print letters in lower case
;;; ----------------------------------------------------------------
printLower:
forLower:	
        push 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]		
	pop   ecx	
	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
	push  ecx
	lea	ecx, [newLine]
	int	0x80
	pop	ecx
	ret