Difference between revisions of "CSC231 Homework Solutions 2017"

From dftwiki3
Jump to: navigation, search
(hw1.asm)
(hw2.asm)
Line 214: Line 214:
 
</source>
 
</source>
 
<br />
 
<br />
 +
=Teller.asm=
 +
<br />
 +
::<source lang="asm">
 +
;;; ; teller.asm
 +
;;; ; D. Thiebaut
 +
;;; ;
 +
;;; ; Demo of a teller machine program.
 +
;;; ; To assemble, link, and run:
 +
;;; ;    nasm -f elf  teller.asm
 +
;;; ;    ld -melf_i386 -o teller teller.o 231Lib.o
 +
;;; ;    ./teller
 +
;;; ;               
 +
 +
;;;  extern functions that will be linked to this program
 +
;;; ; contained in 231Lib.asm
 +
 +
extern  _printDec
 +
extern  _printString
 +
extern  _println
 +
extern  _getInput
 +
 +
;;; ------------------------------------------------------
 +
;;; DATA SECTION
 +
;;; ------------------------------------------------------       
 +
                section .data
 +
amount          dd      139
 +
no20s          dd      0
 +
no10s          dd      0
 +
no5s            dd      0
 +
no1s            dd      0
 +
 +
msg            db      "amount? "
 +
MSGLEN          equ    $-msg       
 +
 +
;;; ------------------------------------------------------
 +
;;; CODE SECTION
 +
;;; ------------------------------------------------------       
 +
                section .text
 +
                global  _start
 +
       
 +
_start:
 +
 +
;;; get amount from the user
 +
                mov    ecx, msg
 +
                mov    edx, MSGLEN
 +
                call    _printString
 +
                call    _getInput
 +
                mov    dword[amount], eax
 +
        call _println
 +
 +
;;; Break down amount in 20s, 10s, 5s, and 1s
 +
                mov    eax, dword[amount]
 +
                mov    edx, 0
 +
 +
                mov    ebx, 20
 +
                mov    eax, dword[amount]
 +
                call    break
 +
                mov    dword[no20s], eax
 +
 +
                mov    ebx, 10
 +
                mov    eax, dword[amount]
 +
                call    break
 +
                mov    dword[no10s], eax
 +
       
 +
                mov    ebx, 5
 +
                mov    eax, dword[amount]
 +
                call    break
 +
                mov    dword[no5s], eax
 +
                mov    eax, dword[amount]
 +
                mov    dword[no1s], eax
 +
 +
                mov    eax, dword[no20s]
 +
                call    _printDec
 +
                call    _println
 +
                mov    eax, dword[no10s]
 +
                call    _printDec
 +
                call    _println
 +
                mov    eax, dword[no5s]
 +
                call    _printDec
 +
                call    _println
 +
                mov    eax, dword[no1s]
 +
                call    _printDec
 +
                call    _println
 +
       
 +
 +
;;; exit
 +
                mov    ebx, 0
 +
                mov    eax, 1
 +
                int    0x80
 +
 +
;;; sum: gets 2 ints in eax and ebx and returns the sum in
 +
;;; eax.
 +
sum:            add    eax, ebx,
 +
                ret
 +
 +
;;; break: gets amount in eax, divider in ebx, divides,
 +
;;; and puts remainder in [amount] and quotient in eax
 +
break:          mov    edx, 0
 +
                div    ebx
 +
                mov    dword[amount], edx
 +
                ret
 +
 +
</source>

Revision as of 17:18, 22 April 2017

--D. Thiebaut (talk) 17:57, 22 April 2017 (EDT)


Problem 1

;;; mystery.asm 
;;; D. Thiebaut  
;;;                
;;; To assemble, link, and run:  
;;;     nasm -f elf  mystery.asm  
;;;     ld -melf_i386 -o mystery mystery.o 
;;;     ./mystery        
;;;      

                section .data
Hello           db      "Hello there!", 10, 10
HelloLen        equ     $-Hello
	
                section .text
                global  _start
_start:

;;; print Hello and a space after it
	
                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, Hello      ; address of message to print    
                mov     edx, 6          ; print only 6 chars
                int     0x80

;;; print ! and two line feed chars

                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, Hello+11   ; address of ! char
                mov     edx, 3          ; print only 3 chars
                int     0x80

;;; exit                                                                                                                                    
                mov     ebx, 0
                mov     eax, 1
                int     0x80


hw1.asm


;;; hw1.asm
;;; D. Thiebaut  
;;;                
;;; To assemble, link, and run:  
;;;     nasm -f elf  hw1.asm  
;;;     ld -melf_i386 -o hw1 hw1.o 
;;;     ./hw1        
;;;      

                section .data
msg             db      "  CSC231 Assembly "
linefeed	db	10
dashline        db	"------------------"
lineLen		equ	$-dashline
	
                section .text
                global  _start
_start:

;;; print message                                                                                                                           
                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, dashline   ; address of message to print    
                mov     edx, lineLen   	; # of chars to print  
                int     0x80

                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, dashline   ; address of message to print    
                mov     edx, lineLen   	; # of chars to print  
                int     0x80

		mov	eax, 4		;print line-feed
		mov	ebx, 1
		mov	ecx, linefeed
		mov	edx, 1
		int	0x80

                mov     eax, 4		; write ----
		mov     ebx, 1 		; stdout
		mov     ecx, dashline 	; address of message to print
		mov     edx, 9  	; # of chars to print
		int     0x80

                mov     eax, 4          ; write message
                mov     ebx, 1          ; stdout  
                mov     ecx, msg 	; address of message to print    
                mov     edx, lineLen    ; # of chars to print  
                int     0x80
	
                mov     eax, 4		; write ----
		mov     ebx, 1 		; stdout
		mov     ecx, dashline 	; address of message to print
		mov     edx, 9  	; # of chars to print
		int     0x80

		mov	eax, 4		;print line-feed
		mov	ebx, 1
		mov	ecx, linefeed
		mov	edx, 1
		int	0x80

                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, dashline   ; address of message to print    
                mov     edx, lineLen   	; # of chars to print  
                int     0x80

                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, dashline   ; address of message to print    
                mov     edx, lineLen   	; # of chars to print  
                int     0x80

		mov	eax, 4		;print line-feed
		mov	ebx, 1
		mov	ecx, linefeed
		mov	edx, 1
		int	0x80

;;; exit                                                                                                                                    
                mov     ebx, 0
                mov     eax, 1
                int     0x80


hw2.asm


;;; ; ; hw2sol.asm
;;; ; ; D. Thiebaut
;;; ; ;
;;; ; ;


	        extern  _printDec
	        extern  _printString
	        extern  _println
	        extern  _getInput
	
	section	.data
prompt		db	"> "
promptLen	equ	$-prompt	
ansStr          db      "ans = "
ansStrLen	equ	$-ansStr	

a		dd	0
b		dd	0
c		dd	0
ans		dd	0
	
		section	.text
		global	_start
_start:
	;; display prompt
		mov	ecx, prompt
		mov	edx, promptLen
		call	_printString
	;; get a
		call	_getInput
		mov	dword[a], eax

	;; display prompt
		mov	ecx, prompt
		mov	edx, promptLen
		call	_printString
	;; get b
		call	_getInput
		mov	dword[b], eax
	
	;; display prompt
		mov	ecx, prompt
		mov	edx, promptLen
		call	_printString
	;; get c
		call	_getInput
		mov	dword[c], eax
	
	;; -----------------------------------
	;; computation: ans = 2*(a-b) + 3*c
	;; -----------------------------------
	
	; your code will go here...
	

	;; -----------------------------------
	;; display "ans ="
	;; -----------------------------------
		mov	ecx, ansStr
		mov	edx, ansStrLen
		call	_printString

	;; -----------------------------------
	;; display ans variable
	;; -----------------------------------
		mov	eax, dword[ans]
		call	_printDec
		call	_println
		call	_println
	
;;; exit
		mov	ebx, 0
		mov	eax, 1
		int     0x80


Teller.asm


;;; ; teller.asm
;;; ; D. Thiebaut
;;; ;
;;; ; Demo of a teller machine program.
;;; ; To assemble, link, and run:
;;; ;     nasm -f elf  teller.asm
;;; ;     ld -melf_i386 -o teller teller.o 231Lib.o
;;; ;     ./teller
;;; ;                

;;;  extern functions that will be linked to this program
;;; ; contained in 231Lib.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput

;;; ------------------------------------------------------
;;; DATA SECTION
;;; ------------------------------------------------------        
                section .data
amount          dd      139
no20s           dd      0
no10s           dd      0
no5s            dd      0
no1s            dd      0

msg             db      "amount? "
MSGLEN          equ     $-msg        

;;; ------------------------------------------------------
;;; CODE SECTION
;;; ------------------------------------------------------        
                section .text
                global  _start
        
_start: 

;;; get amount from the user
                mov     ecx, msg
                mov     edx, MSGLEN
                call    _printString
                call    _getInput
                mov     dword[amount], eax
        	call	_println
	
;;; Break down amount in 20s, 10s, 5s, and 1s
                mov     eax, dword[amount]
                mov     edx, 0

                mov     ebx, 20
                mov     eax, dword[amount]
                call    break
                mov     dword[no20s], eax

                mov     ebx, 10
                mov     eax, dword[amount]
                call    break
                mov     dword[no10s], eax
        
                mov     ebx, 5
                mov     eax, dword[amount]
                call    break
                mov     dword[no5s], eax
                mov     eax, dword[amount]
                mov     dword[no1s], eax

                mov     eax, dword[no20s]
                call    _printDec
                call    _println
                mov     eax, dword[no10s]
                call    _printDec
                call    _println
                mov     eax, dword[no5s]
                call    _printDec
                call    _println
                mov     eax, dword[no1s]
                call    _printDec
                call    _println
        

;;; exit
                mov     ebx, 0
                mov     eax, 1
                int     0x80

;;; sum: gets 2 ints in eax and ebx and returns the sum in
;;; eax.
sum:            add     eax, ebx,
                ret

;;; break: gets amount in eax, divider in ebx, divides,
;;; and puts remainder in [amount] and quotient in eax
break:          mov     edx, 0
                div     ebx
                mov     dword[amount], edx
                ret