CSC231 Print binary contents of DWord
--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...