Difference between revisions of "CSC231 Homework 7 Solution Programs"
(→Program 1) |
|||
Line 136: | Line 136: | ||
ret | ret | ||
</source> | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[category:CSC231]][[Category:Homework]] |
Revision as of 12:43, 1 November 2012
--D. Thiebaut 13:21, 1 November 2012 (EDT)
Program 1
- This solution is from Gavi and shows she got the hints I gave the class about this array of character. The trick here is that since all the characters are listed in order in the string, you have character '0' at index 0 in the string. Character '1' at Index 1. Character '2' at Index 2... Character '9' at Index 9. And now for the magic: Character 'A' is at Index 10. Character 'B' at Index 11, all the way to Character 'F' at Index 15. So we do not need to test whether the number we want to print in hex is less than 10 or not. We just take the number, use it as an index in the string and pull the character that corresponds to it!
;;; ; hw7a.asm
;;; ; Gavi Levy Haskell
;;; ; 231a-ae
;;; ;
;;; ;
;;; ; Displays the contents of a double-word in decimal and
;;; ; hexadecimal.
;;; ;
;;; ; Prints:
;;; ; x = 305441741 = 0x1234ABCD
;;; ;
;;; ;
;;; ; nasm -f elf -F stabs hw7a.asm
;;; ; gcc -m32 -o hw7a driver.c asm_io.o hw7a.o
;;; ; ./hw7a
%include "asm_io.inc"
;; -----------------
;; DATA SECTION
;; -----------------
section .data
x dd 0x1234ABCD
msg dd " x = ", 0x00
msg2 dd " = 0x", 0x00
hexChr db "0123456789ABCDEF"
;; -----------------
;; CODE SECTION
;; -----------------
section .text
global asm_main
asm_main:
mov eax, msg
call print_string ; print " x = "
mov eax, dword[x]
call print_int ; print "305441741"
mov eax, msg2
call print_string ; print " = 0x"
mov ecx, 8
for: mov ebx, dword[x] ; ebx <-- current rotation of x
rol ebx, 4 ; rotate first byte to end
mov dword[x], ebx ; save current rotation in x
and ebx, 0x000000F ; keep only last digit in ebx
mov al, byte[hexChr + ebx]
call print_char ; print the character of hexChr
; corresponding to the digit
loop for
call print_nl
;; return to C program
ret
Another good solution that doesn't use the array is provided by Naomi:
;;; hw7a.asm
;;; 231a-ad
;;; Naomi Long
;;;
;;; This program prints the decimal representation of the hex number
;;; 0x1234ABCD and then prints out the hex digits. The output is shown
;;; below, sandwiched between two prompts:
;;;
;;; [231a-ad@grendel ~/hw7]$ ./hw7a
;;; x = 305441741 = 0x1234ABCD
;;; [231a-ad@grendel ~/hw7]$
%include "asm_io.inc"
;;; -------------------------
;;; data segment
;;; -------------------------
section .data
x dd 0x1234ABCD
xeq db "x = ",0x00
eq db " = 0x",0x00
;;; -------------------------
;;; code area
;;; -------------------------
section .text
global asm_main
asm_main:
;;; print "x = "
mov eax, xeq
call print_string ; prints "x = "
;;; print decimal representation and " = 0x"
mov eax, dword[x]
call print_int ; prints 305441741
mov eax, eq
call print_string ; prints " = 0x"
;;; print hex representation
mov ecx, 8 ; 8 hex digits to loop over
start:
mov eax, dword[x] ; gets current hex rotation
rol eax, 4 ; moves first hex to last position
mov dword[x], eax ; saves rotation
and eax, 0x0000000f ; remove all but last hex digit
cmp eax, 10 ; check if hex digit letter or number
jae letr ; jump to treat letter as ascii char
call print_int ; prints last hex digit if number
jmp loop ; skip to end of loop
letr: add eax, 'A'-10 ; offset dec to ascii character
call print_char ; prints last hex number as ascii char
loop: loop start ; loop to print all 8 hex digits
call print_nl ; prints new line after loop is done
;;; return to C program
ret