News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to make hexadecimal input?

Started by bocin, December 25, 2010, 01:50:23 PM

Previous topic - Next topic

Vortex

Hi bocin,

Could you give more details about your question?

bocin

i want to make a converter program from hexadecimal value to binary value

but i can't make the input to be read as hexadecimal value

maybe you can take a look at this coding below

INCLUDE Irvine32.inc

BUFFER_SIZE = 501

.data
buffer1 DWORD BUFFER_SIZE DUP(?)
buffer BYTE 32 dup(0),0
bufsize DWORD ?

.code
main PROC
;input
   mov   ecx, BUFFER_SIZE      ; Input Nama
   mov   edx, OFFSET buffer1
   call   ReadString
   mov  bufSize,eax              ; save the length

   mov   eax,buffer1      ; number to display
   mov   ecx,32         ; number of bits in EAX
   mov   esi,offset buffer

L1:   shl   eax,1         ; shift high bit into Carry flag
   mov   BYTE PTR [esi],'0'   ; choose 0 as default digit
   jnc   L2            ; if no Carry, jump to L2
   mov   BYTE PTR [esi],'1'   ; else move 1 to buffer

L2:   inc  esi            ; next buffer position
   loop L1            ; shift another bit to left

   mov  edx,OFFSET buffer   ; display the buffer
   call WriteString
   call Crlf
   exit
main ENDP
END main

I'm a newbie here.. thx for your attention

dedndave

the buffer should always be BUFFER_SIZE+1
buffer1 DWORD BUFFER_SIZE+1 DUP(?)
but, that isn't the major problem   :P
let me play a little bit...

dedndave

well - you aren't taking into consideration that the input string is ASCII

for ASCII digits 0-9, you convert them to binary by AND'ing out the ASCII bits, leaving only the 4 low-order bits
for ASCII hex digits A-F, you force them to either be all upper-case or all lower-case, then subtract

once you have done that, each hex digit will occupy 4 bits in the accumulator

a 500 byte buffer will make a very large binary value - lol
even though the buffer is large, you can limit the user input by telling ReadString that the buffer is only 8 bytes
this will limit the input value to 8 ASCII hex digits

dedndave

are you wanting a binary ASCII string ???
like this...

10101010101010101010101010101010

or do you want a 32-bit binary value

bocin

i want a binary ASCII string like you've written..


dedndave

i have no simple way to test this, as i do not have Kip's lib handy
and - there are faster and more efficient ways of doing it
but, i wanted to keep it simple so you could see how it works
main PROC
;input
   mov   ecx, 8                  ; max input length
   mov   edx, OFFSET buffer1
   call   ReadString


        mov     ecx,eax
        mov     esi,offset buffer1
        mov     edi,offset buffer

outerLoop:
        lodsb
        cmp     al,30h                 ;ASCII '0'
        jb      notHexDigit

        cmp     al,39h                 ;ASCII '9'
        ja      notDecimalDigit

        and     al,0Fh                 ;convert ASCII decimal to binary
        jmp short doFourBits

notDecimalDigit:
        and     al,0DFh                ;force upper case
        cmp     al,65                  ;ASCII 'A'
        jb      notHexDigit

        cmp     al,70                  ;ASCII 'F'
        ja      notHexDigit

        sub     al,55                  ;convert 'A' to 10

doFourBits:
        shl     eax,4
        push    ecx
        mov     ecx,4

innerLoop:
        and     eax,0FFh
        shl     eax,1
        or      ah,30h                 ;convert binary to ASCII decimal
        mov     [edi],ah
        inc     edi
        loop    innerLoop

        pop     ecx
        loop    outerLoop

notHexDigit:
        mov byte ptr [edi],0

   mov  edx,OFFSET buffer   ; display the buffer
   call WriteString
   call Crlf
   exit
main ENDP

try it out and let me know if it works   :P

EDIT - the buffer does not need to be initialized to 0's
all of the data you currently have defined could be in the .DATA? section
buffer1 should be BYTE - not DWORD
.data?
buffer1 BYTE BUFFER_SIZE+1 DUP(?)
buffer BYTE 33 dup(?)
bufsize DWORD ?

bocin

hey it works.. thx

actually, all the code I've be write before is the example program from irvine book..
and I want to modify it..

thank you very much.. I will study carefully the code you give to me..