CSC231 Homework 3 Solutions 2010

From dftwiki3
Revision as of 09:19, 15 October 2010 by Thiebaut (talk | contribs) (hw3a.asm)
Jump to: navigation, search

--D. Thiebaut 14:18, 15 October 2010 (UTC)


hw3a.asm

;;; ; ; Ashley Smith
;;; ; ; 231a-ae
;;; ; ; 6 October 2010
;;; ; ; hw3a.asm (Problem #1 of Homework 3)
;;; ; ;
;;; ; ; Moves the contents of an array of bytes (tableb) into an
;;; ; ;	array of words (tablew) using bytes, words, and double words.
;;; ; ; Use DDD to see the results of the movement.
;;; ; ; "->" in commenhts means move to; B means byte
;;; ; ;
;;; ; ; to assemble and run:
;;; ; ;
;;; ; ;    nasm -f elf -F stabs hw3a.asm
;;; ; ;    ld -melf_i386 -o hw3a hw3a.o
;;; ; ;    ./hw3a
;;; ; ;    ddd hw3a &
;;; ; ;
;;; ; ; ------------------------------------------------------------

EXIT		equ	1
WRITE		equ	4
STDOUT		equ	1
	
        ;; ------------------------------------------------------------
        ;; data area
        ;; ------------------------------------------------------------

		section .data
tableb		db	1, 10, 0xff, 5, 0x0a 	; array to move
tablew		dw	0, 0, 0, 0, 0		; array to move into

        ;;  ------------------------------------------------------------
        ;;  code area
        ;;  ------------------------------------------------------------

		section .text 
		global _start

_start:		nop
		nop

		;; using a byte
		add	ah, byte[tableb] 	; add "1" to ah
		mov	byte[tablew], ah 	; ah -> 1st B of tablew

		;; using a word
		mov	ax, word[tableb+1] 	; "10" and "0xff" -> ax
		mov	byte[tablew+1*2], al 	; "10" -> 3rd B of tablew
		mov	byte[tablew+2*2], ah 	; "0xff" -> 5th B of tablew

		;; using a double word
		mov	eax, dword[tableb+3] 	; "5", "0x0a", "01" -> eax
		mov	byte[tablew+3*2], al   	; "5" -> 7th B of tablew
		mov	byte[tablew+4*2], ah   	; "0x0a" -> 9th B of tablew
	
		;; exit()
		mov	eax, EXIT
		mov	ebx, 0
		int	0x80			; final system call

hw3b.asm

;;; ; ; ; Ashley Smith
;;; ; ; ; 231a-ae
;;; ; ; ; 6 October 2010
;;; ; ; ; hw3b.asm (Problem #2 of Homework 3)
;;; ; ; ;
;;; ; ; ; Moves the contents of an array of bytes (tableb) into an
;;; ; ; ; array of double words (tabled) using bytes, words, and
;;; ; ; ; double words. Use DDD to see the results of movement.
;;; ; ; ; "->" in comments means move to; B means byte
;;; ; ; ;
;;; ; ; ; to assemble and run:
;;; ; ; ;
;;; ; ; ;    nasm -f elf -F stabs hw3b.asm
;;; ; ; ;    ld -melf_i386 -o hw3b hw3b.o
;;; ; ; ;    ./hw3b
;;; ; ; ;    ddd hw3b &
;;; ; ; ;
;;; ; ; ; ------------------------------------------------------------

EXIT            equ	1
WRITE           equ     4
STDOUT          equ     1

	;;;  ------------------------------------------------------------
	;;;  data area
	;;;  ------------------------------------------------------------    

		section .data
tableb		db	1, 10, 0xff, 5, 0x0a 	; array to move
tabled		dd	0, 0, 0, 0, 0	     	; array to move into

	;;;   ------------------------------------------------------------
	;;;   code area
	;;;   ------------------------------------------------------------

		section .text
		global _start

_start:		nop
		nop

		;; using a double word
		mov	eax, dword[tableb] 	; "1", "10", and "ff" -> eax
		mov	byte[tabled], al   	; "1" -> 1st B of tabled
		mov	byte[tabled+1*4], ah 	; "10" -> 4th B of tabled

		;; using a byte
		mov	ah, byte[tableb+2] 	; "ff" -> ah
		mov	byte[tabled+2*4], ah 	; ah -> 8th B of tabled

		;; using a word
		mov	ax, word[tableb+3] 	; "5" and "0a" -> ax
		mov	byte[tabled+3*4], al 	; "5" -> 12th B of tabled
		mov	byte[tabled+4*4], ah 	; "0a" -> 16th B of tabled
	
		;; exit()
		mov	eax, EXIT
		mov	ebx, 0
		int	0x80	; final system call

hw3c.asm

;;; ; ; ; ; Ashley Smith
;;; ; ; ; ; 231a-ae
;;; ; ; ; ; 6 October 2010
;;; ; ; ; ; hw3c.asm (Problem #3 of Homework 3)
;;; ; ; ; ;
;;; ; ; ; ; Moves the least significant byte of each word in an array
;;; ; ; ; ; of double words (tabled) into an array of bytes (tableb),
;;; ; ; ; ; at the same index in tableb as the byte was in in tabled.
;;; ; ; ; ; Use DDD to see results of movement.
;;; ; ; ; ; "->" in comments means move to; B means byte
;;; ; ; ; ;
;;; ; ; ; ; tableb in memory (hex):
;;; ; ; ; ; 0x01 0x02 0xff 0x00 0x78 0x21
;;; ; ; ; ;
;;; ; ; ; ; to assemble and run:
;;; ; ; ; ;
;;; ; ; ; ;    nasm -f elf -F stabs hw3c.asm
;;; ; ; ; ;    ld -melf_i386 -o hw3c hw3c.o
;;; ; ; ; ;    ./hw3c
;;; ; ; ; ;    ddd hw3c &
;;; ; ; ; ;
;;; ; ; ; ; ------------------------------------------------------------

EXIT            equ     1
WRITE           equ     4
STDOUT          equ     1

	;;; ;  ------------------------------------------------------------
	;;; ;  data area
	;;; ;  ------------------------------------------------------------

	                section .data
		;; array to move
tabled		dd	1, 2, 255, 256, 0x12345678, 0x87654321
		;; array to move into
tableb		db	0, 0, 0, 0, 0, 0
	
	;;; ;   ------------------------------------------------------------
	;;; ;   code area
	;;; ;   ------------------------------------------------------------

	                section .text
	                global _start

_start:		nop
	        nop
	
		;; move 01 into [tableb]
		mov	ax, word[tabled] 	; 0001 -> ax
		mov	byte[tableb], al 	; 01 -> 1st B of tableb

		;; move 02 into [tableb+1]
		mov	ax, word[tabled+4] 	; 0002 -> ax
		mov	byte[tableb+1], al 	; 02 -> 2nd B of tableb

		;; move 255 into [tableb+2]
		mov	ax, word[tabled+8] 	; 00ff (hex of 255) -> ax
		mov	byte[tableb+2], al  	; 00ff -> 3rd B of tableb

		;;  move 256 into [tableb+3]
		mov	ax, word[tabled+12] 	; move 0100 (hex of 256) -> ax
		mov	byte[tableb+3], al  	; move 00 -> 4th B of tableb

		;; move 5678 into [tableb+4]
		mov	ax, word[tabled+16] 	; 5678 -> ax
		mov	byte[tableb+4], al  	; 78 -> 5th B of tableb

		;; move 4321 into [tableb+5]
		mov	ax, word[tabled+20] 	; 4321 -> ax
		mov	byte[tableb+5], al  	; 21 -> 6th B of tableb
	
;;;  exit()
	        mov     eax, EXIT
	        mov     ebx, 0
	        int     0x80 ; final system call