The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: brixton on June 22, 2005, 02:11:38 PM

Title: Hex to ASCII
Post by: brixton on June 22, 2005, 02:11:38 PM
Hey all,

I have a Hex number of an ASCII character stored like this:

number dd 61h

This is for the letter 'a'.  I want to put this in a buffer like this

buffer db 1 dup (?)

How would I do such a thing?

Thanks in advance!
Title: Re: Hex to ASCII
Post by: hutch-- on June 22, 2005, 02:23:45 PM
The "hex" is just a DWORD in this context, not an ASCII hex string so just use a DWORD to ASCII conversion.
Title: Re: Hex to ASCII
Post by: brixton on June 22, 2005, 02:53:15 PM
Thanks for the reply!

I have used

invoke dwtoa, numbah, ADDR buffer
invoke StdOut, ADDR buffer


But now it prints out the ASCII value numbah, so in the above example it would print out 97 (the ASCII code for 'a') :(

How can I get it to print out the actual value and not just the number?   :red
Title: Re: Hex to ASCII
Post by: Mark Jones on June 22, 2005, 02:55:09 PM
Hey Brixton, check out this post under valfound:
http://www.masmforum.com/simple/index.php?topic=431.msg15809#msg15809
Title: Re: Hex to ASCII
Post by: roticv on June 22, 2005, 02:56:59 PM
mov eax, number
mov byte ptr [buffer],al

Sorry if i got the syntax wrong, I'm rusty. Coding too much C.
Title: Re: Hex to ASCII
Post by: hutch-- on June 22, 2005, 03:02:33 PM
You have to look at you original example again, you specified the number as a DWORD, not a BYTE and if you want to display the first BYTE of the DWORD, you need to write that BYTE to a DB buffer and append a zero to the end of it then display it as a zero terminated string. Victor's example works fine.
Title: Re: Hex to ASCII
Post by: brixton on June 22, 2005, 03:26:23 PM
Many thanks for the help  :U

Sorry about my noobness..  it shall receed in time  :thumbu
Title: Re: Hex to ASCII
Post by: AeroASM on June 22, 2005, 03:56:43 PM
Quote from: brixton on June 22, 2005, 02:53:15 PM
Thanks for the reply!

I have used

invoke dwtoa, numbah, ADDR buffer
invoke StdOut, ADDR buffer


But now it prints out the ASCII value numbah, so in the above example it would print out 97 (the ASCII code for 'a') :(

How can I get it to print out the actual value and not just the number?   :red

The actual value IS a number.

I think that you want the hex value (61h) rather than decimal (97).
If so then use dwtoah not dwtoa.
Title: Re: Hex to ASCII
Post by: brixton on June 22, 2005, 10:08:34 PM
Quote from: Mark Jones on June 22, 2005, 02:55:09 PM
Hey Brixton, check out this post under valfound:
http://www.masmforum.com/simple/index.php?topic=431.msg15809#msg15809

That is just what I need but I can't include macros.asm or I get a million errors..  where do I put this in the includes?  If I put it at the top I get lots of errors, same with middle, same with end..  :tdown
Title: Re: Hex to ASCII
Post by: brixton on June 22, 2005, 11:43:14 PM
Quote from: roticv on June 22, 2005, 02:56:59 PM
mov eax, number
mov byte ptr [buffer],al

Sorry if i got the syntax wrong, I'm rusty. Coding too much C.

OK this works.. Now I understand a little more about the sizes.  AX is 32/2 bits big, and AL is the lower half of this, so 32/4 bits which is 8.  This is a byte :)  Now I just need to figure out what byte ptr is and how it works..  and why i can't just mov buffer, al  :P  It's all very interesting!

edit:  Apologies for the double-post.
Title: Re: Hex to ASCII
Post by: AeroASM on June 23, 2005, 07:29:48 AM
You CAN use mov buffer,al. MASM knows that you want to move a byte becausae al is a BYTE-SIZED (roflmao!) register.
You need to use byte ptr etc when you move an immed into a mem.

mov myvar,8                  -move a byte, word or dword?
mov word ptr [offset myvar],8    -move the word 0008h into buffer.
Title: Re: Hex to ASCII
Post by: brixton on June 23, 2005, 12:37:17 PM
Quote from: AeroASM on June 23, 2005, 07:29:48 AM
You need to use byte ptr etc when you move an immed into a mem.

Thanks for the reply but what does that mean?  :eek :'(
If I understand this line then I can understand why I can't use mov buffer, al  :green2
Title: Re: Hex to ASCII
Post by: AeroASM on June 23, 2005, 12:51:19 PM
There are three types of operands:

1. reg = A register like eax, bl, ch, dx, si, esp
2. mem = A place in memory; could be the name of a variable or could be an offset in brackets e.g. [esi] or [ecx+eax*4+5]
3. immed = A constant value.

The addressing modes of mov are as thus:

mov reg,reg
mov reg,mem
mov mem,reg
mov reg,immed
mov mem,immed

Note that mov mem,mem is not allowed, and immed cannot be the first operand.
Instructions like add, sub have similar addressing modes.

In all of the above mov addressing modes, the CPU needs to know what size of data it should move. If MASM knows the size then it can encode it for you. If one of the operands is a reg then MASM knows what size it is:

mov [esi],eax              -MASM knows to move a DWORD
mov myvar,al               -MASM knows to move a BYTE

The only addressing mode for mov above that does not have a reg is mov mem,immed. Therefore when moving a constant into a memory operand you need to specify what size you want to move:

mov byte ptr [esi],8     -MASM knows to move a BYTE.

The reason that you cannot mov buffer,al is that buffer was declared as a DWORD, and al is a BYTE, so MASM complains. You can override the assumption that buffer is a DWORD by putting byte ptr, so it works.
Title: Re: Hex to ASCII
Post by: brixton on June 23, 2005, 09:03:44 PM
Thankyou for that, very kind of you to take your time explaining!  It makes sense!  :dance: :bg

BTW why does my BYTE buffer have to be 2 bytes big?  Otherwise it doesn't work..  is it due to a zero teminator or something?
Title: Re: Hex to ASCII
Post by: roticv on June 24, 2005, 02:00:51 AM
What do you mean by it cannot work? Do you mean you can't MessageBox with buffer? If it is so, it is because MessageBox requires a string, while buffer is at most a byte/byte array. You need room for a null, for it to be a string.
Title: Re: Hex to ASCII
Post by: Dark Schneider on June 24, 2005, 06:56:21 AM
It CAN be one byte, as long as the following byte is zero.
Title: Re: Hex to ASCII
Post by: AeroASM on June 24, 2005, 07:01:32 AM
IN which case the following byte would be part of the buffer becasue you cannot use it for anything else and the buffer is two bytes long.
Title: Re: Hex to ASCII
Post by: brixton on June 24, 2005, 07:09:14 AM
Makes sense, thankyou, yes that's what I meant  :green2

You're good at deciphering noob-talk  :thumbu :bg
Title: Re: Hex to ASCII
Post by: AeroASM on June 24, 2005, 07:27:48 AM
I was once a n00b myself  :U :wink