CSC231 Homework 3 Solutions Fall 2012

From dftwiki3
Revision as of 09:10, 1 October 2012 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Problem #1= <code><pre> Julia B. Edwards 231a-aa September 20, 2012 hw3a.txt Initially creates: eax: 00000000 ebx: 12345678 ecx: 87654321 edx: ffffffff Sourc...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 10:10, 1 October 2012 (EDT)


Problem #1


Julia B. Edwards
231a-aa
September 20, 2012

hw3a.txt

Initially creates:


eax: 00000000
ebx: 12345678
ecx: 87654321
edx: ffffffff

Sources and Destinations:


1) bl = 78 --> al
2) ch = 43 --> ah
3) dh = ff --> dl
4) bl = 78 --> dh
5) cx = 4321 --> bx
6) 'a' = 61 --> ch
7) 'a'+32 = 'a' + 20 (20 in hex = 32 in decimal) = 81 --> cl

New values in the reigsters:


eax: 00004378 (altered by steps 1 and 2)
ebx: 12344321 (altered by step 5)
ecx: 87656181 (altered by steps 6 and 7)
edx: ffff78ff (altered by steps 3 and 4)

Problem #2

;;; hw3b.asm
;;; Naomi Long (edited by D. Thiebaut)
;;; 231a-ad
;;;
;;; This program copies a string into another string
;;; without modifying any other variable's contents.
;;; The new string is output sandwiched between two different
;;; variables to show the lack of side-effects.
;;; 
;;; [231a-ad@grendel ~/hw3]$ ./hw3b
;;; CSC231 Assembly
;;;
;;; CSC231 Assembly
;;; 
;;; Fall 2012
;;;
;;; [231a-ad@grendel ~/hw3]$ 
;;;
;;; To run:
;;; 	nasm -f elf -F stabs hw3b.asm
;;; 	ld -melf_i386  -o hw3b hw3b.o
;;; 	./hw3b

	
 	    section	.data
msg1        db             "CSC231 Assembly", 10, 10
msg1Len     equ            $-msg1
msg2        db             "................", 10
msg2Len     equ            $-msg2
msg3        db             "Fall 2012", 10, 10
msg3Len     equ            $-msg3


	section	.text
	global	_start
_start:
;;; move "CSC2" into msg2
	mov 	eax, dword[msg1]
	mov 	dword[msg2], eax

;;; move "31 A" into msg2
	mov 	eax, dword[msg1+4]
	mov	dword[msg2+4], eax

;;; move "ssem" into msg2
	mov	eax, dword[msg1+8]
	mov	dword[msg2+8], eax

;;; move "bly", 0x10 into msg2
	mov 	eax, dword[msg1+12]
	mov	dword[msg2+12], eax

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

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

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

;;; exit and return to Operating System
	mov	ebx, 0		; return code 0: everything Ok!
	mov	eax, 1
	int	0x80

Problem #3

;;; ; Julia B. Edwards
;;; ; 231a-aa
;;; ; hw3c.asm
;;; ;
;;; ; This program prints out the following:
;;; ;
;;; ; I love chocolate
;;; ; J'aime chocolat!
;;; ;
;;; ;
;;; ; by altering msg1 ("I love chocolate", 10) after it has
;;; ; been printed.
;;; ;
;;; ;
;;; ; To compile, link, and run:
;;; ;     nasm -f elf -F stabs hw3c.asm
;;; ;     ld -melf_i386 -o hw3c hw3c.o
;;; ;     ./hw3c     
;;; ;
	
	section	.data

msg1		db	"I love chocolate", 10
msg1Len		equ	$-msg1


		section	.text
		global	_start
_start:

;;; print original string
	        mov	eax, 4
		mov	ebx, 1
		mov	ecx, msg1
		mov	edx, msg1Len
		int	0x80
	
;;; Modify the string by replacing key characters.

		mov	eax, "J'ai"
		mov	dword[msg1], eax 	;change first 4 chars of msg1

		mov	ah, "m"
		mov	byte[msg1+4], ah 	;change 5th char of msg1

		mov	ah, "!"
		mov	byte[msg1+msg1Len-2], ah ;change last char of msg1
	
;;; print modified string
	        mov	eax, 4
		mov	ebx, 1
		mov	ecx, msg1
		mov	edx, msg1Len
		int	0x80

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