Difference between revisions of "CSC231 Print binary contents of DWord"
Line 86: | Line 86: | ||
shl eax, 1 | shl eax, 1 | ||
mov ebx, eax | mov ebx, eax | ||
− | + | mov eax, 0 ; eax <-- 0 | |
− | + | ;;; it's tempting to use the xor below to clear eax, but it won't work... | |
+ | ;;; Figure out why... | ||
+ | ;;; xor eax, eax | ||
adc eax, 0 ; eax <-- 0 if no carry, 1 if carry | adc eax, 0 ; eax <-- 0 if no carry, 1 if carry | ||
call print_int | call print_int |
Revision as of 08:49, 22 October 2012
--D. Thiebaut 09:45, 22 October 2012 (EDT)
Exercise
- Print the contents of a dword in binary.
- Use the skeleton below to get started:
;;; printBinary.asm
;;; D. Thiebaut
;;; prints a 32-bit number in binary.
;;;
;;; To assemble, link and run:
;;; nasm -f elf printBinary.asm
;;; gcc -m32 -o printBinary printBinary.o driver.c asm_io.o
;;; ./printBinary
%include "asm_io.inc"
;; -------------------------
;; data segment
;; -------------------------
section .data
x dd 0xff55aa11
;; -------------------------
;; code area
;; -------------------------
section .text
global asm_main
asm_main:
mov ebx, dword[x] ; number to print
mov ecx, 32 ; # of bits
;; put your code below this line...
;; return to C program
ret
Some Variations
- An neat trick to print 1 or 0 depending on the value of the bit shifted out (1 or 0) is to use the ADC instruction
- At some point you will need to store 0 in eax then add 1 to it if the bit shifted out is 1. You then have the choice between several ways of clearing eax:
- mov eax, 0
- xor eax, eax
- Figure out why xor eax, eax might be problematic...