--------------------------------------------------------------------------------

I need to convert a number to binary. I know that I can take a number and write it to output (in dos) as binary but don't know how to assign that binary number to a variable.

Can someone point me in the right direction?


INCLUDE Irvine32.inc

CR = 0Dh ;carriage return

LF = 0Ah ;line feed

.data
myMessage1 BYTE "        Welcome to MyProgram that Multiplies a number",0
myMessage2 BYTE "      from the keyboard by 26 using the binary Left Shift in Assembler",0
enterHex BYTE " Please enter a number in hex and I will multiply it by decimal 26: ", 0
binMessage BYTE "    The binary number is: ", 0
hexMessage BYTE "    The hexidemcial number is: ", 0
decMessage BYTE "    The decimal number is: ", 0
num DWORD 0

testMessage BYTE " Please enter your age in hex: ", 0

.code
main PROC

mov edx,OFFSET myMessage1 ; store welcome message address in edx.Use edx register
call WriteString ; Print welcome message
call Crlf ; new line
call Crlf ; new line

mov edx,OFFSET myMessage2 ; store welcome message address in edx.Use edx register
call WriteString ; Print welcome message
call Crlf ; new line
call Crlf ; new line
call Crlf ; new line

mov edx,OFFSET enterHex ; moves whats in the enterHex to edx
call WriteString ; prints what is in the edx
call ReadHex ; reads a hexadecimal integer from the input
call Crlf ; new line
call Crlf ; new line

mov num,OFFSET enterHex

;MUST CONVERT NUMBER TO BINARY HERE

mov eax,num ; moves number entered into eax
mov ebx,num ; moves number entered into ebx
mov ecx,num ; moves number entered into ecx
shl eax,4 ; shifts left 2 power of 4
shl ebx,3 ; shifts left 2 power of 3
add eax,ebx ; addes ebx to eax
shl ecx,1 ; shifts left 2 power of 1
add eax,ecx ; adds ecx to eax

call DumpRegs ; Dump the registers
call Crlf ; new line
call Crlf ; new line

mov edx,OFFSET binMessage ; moves whats in printBin to edx
call WriteString ; prints the info stating the number in binary
mov edx, eax
call WriteBin ; prints the number in binary
call Crlf ; new line
call Crlf ; new line

mov edx,OFFSET hexMessage ; moves whats in printBin to edx
call WriteString ; prints the info stating the number in binary
mov edx, eax
call WriteHex ; prints the number in binary
call Crlf ; new line
call Crlf ; new line

mov edx,OFFSET decMessage ; moves whats in printBin to edx
call WriteString ; prints the info stating the number in binary
mov edx, eax
call WriteDec ; prints the number in binary
call Crlf ; new line
call Crlf ; new line

call WaitMsg
exit
main ENDP
END main
Posted on 2006-09-12 11:50:06 by Westerberg

--------------------------------------------------------------------------------

I need to convert a number to binary. I know that I can take a number and write it to output (in dos) as binary but don't know how to assign that binary number to a variable.

Can someone point me in the right direction?



This smells like someone's home work assignment.... but I will give you the benifit of the doubt.

...but don't know how to assign that binary number to a variable.


All memory locations store in binary.  Essently everything is binary.  A 32 bit register is just that, 32 binary bits grouped together.


I assume the Irvine32.inc provides the DOS keyboard commands for getting user input.  This will enter as a ASCII string of text, pointed by an address pointer.  To convert the Hex string to Binary, you will have to parse the memory pointer, one byte at a time and convert each byte to its binary couterpart, eight bytes of either 30h or 31h.

Alternately, you can use the SHL and test for Cary bits on each byte from the entered Hex string.  In this case, you wont need to provide 16 - 8 byte output patterns of ASCII 1's/0's.

Best of luck..
Regards,
:NaN:
Posted on 2006-09-12 22:04:33 by NaN