; Jade Yu Cheng ; ICS 312 ; Assignment 6 Exercise 1 ; April 9, 2009 ; This program prompts the user to enter a signed 32-bit integer. The program ; prints out the binary representation of the integer. %include "asm_io.inc" segment .data msg1 db "Enter an integer: ", 0 ; msg1 msg2 db "The binary representation is: ", 0 ; msg2 segment .bss binary resb 32 ; space to store binary representation segment .text global asm_main asm_main: enter 0,0 ; setup pusha ; setup ;;; prompt the user the enter a number and treat it as a 32 bit number. mov eax, msg1 ; print out the prompt message. call print_string call read_int ; read a number and store in eax ;;; convert the number to its binary and store it in binary.. convert: mov ebx, binary ; ebx points to binary add ebx, 31 ; ebx points to the last bit of binary convert_loop: cmp ebx, binary - 1 ; terminate when all 32 bits are written je convert_loop_end shr eax, 1 ; right shift by 1, equavilent as divide by 2 jc write_one ; if carry flag is set wirte 1 to ebx mov [ebx], byte 0 ; otherwise wirte 0 to ebx dec ebx ; decrement ebx to the next spot to write jmp convert_loop write_one: mov [ebx], byte 1 ; write 1 to ebx dec ebx ; decrement ebx to the next spot to write jmp convert_loop convert_loop_end: ;;; print out binary print: mov eax, msg2 ; print out the second message call print_string mov ebx, binary ; let ebx points to binary print_loop: cmp ebx, binary + 32; terminate when it reaches the end je print_loop_end mov eax,0 ; clean up eax mov al, [ebx] ; move the bit in eax call print_int ; print it out inc ebx ; increment the pointer to the next spot jmp print_loop print_loop_end: call print_nl ; print a blank line popa ; cleanup mov eax, 0 ; cleanup leave ; cleanup ret ; cleanup