flat assembler
Message board for the users of flat assembler.

Index > Windows > displaying hex-values

Goto page Previous  1, 2, 3
Author
Thread Post new topic Reply to topic
Ali.Z



Joined: 08 Jan 2018
Posts: 719
Ali.Z 30 Jun 2018, 11:46
damn, i feel like im a dummmmmm.

you are smart, honestly i couldnt think this way:

lods ; loads single byte from esi to al
...
sub al,'0' ; which is 30hex (ascii 30 = 0 || 39 = 9)
cmp ... ; if result is less than Ah (10d)
then store it (first shift of ebx is not counted because bl is 0)
EDIT: forgot to mention that shl x,4 will shift 4 bits which means a nibble
where you can store 0x0F in 1 nibble (4bits)
otherwise subtract 7 ; because:

ascii numbers starts from 30 to 39 hex (0-9)
and ascii chars start from 41, the distance between 39h and 41h is 7
...
loop back til al is 0 (null terminated string)


wow, you are genius .. (im bad in math sorry)
whats great in this, it doesnt matter if its a or A, f or F.

Q. is it important to clear DF?
anyhow bunch of thanks man.


Last edited by Ali.Z on 30 Jun 2018, 16:30; edited 1 time in total
Post 30 Jun 2018, 11:46
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 719
Ali.Z 30 Jun 2018, 15:57
ok seems to be important to clear DF
"When the DF flag is set to 0, string operations increment the index registers" esi/edi

:: BUT when i debugged my flag using olly, it shows the DF was already '0'
maybe i can get rid of cld?
Post 30 Jun 2018, 15:57
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 30 Jun 2018, 22:51
Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
Post 30 Jun 2018, 22:51
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 30 Jun 2018, 22:55
Ali.A wrote:
ok seems to be important to clear DF
"When the DF flag is set to 0, string operations increment the index registers" esi/edi

:: BUT when i debugged my flag using olly, it shows the DF was already '0'
maybe i can get rid of cld?
This is dependant on the OS. Many OSes will start applications with DF=0. Windows does this. But if you want to run your code in a different context then it might not be guaranteed that DF is in any particular state.
Post 30 Jun 2018, 22:55
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 719
Ali.Z 01 Jul 2018, 09:42
meant debugged my program* lol

anyhow, thanks for the info rev.
Post 01 Jul 2018, 09:42
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1392
Location: Piraeus, Greece
Picnic 02 Jul 2018, 08:42
Ali.A wrote:
damn, i feel like im a dummmmmm.

you are smart, honestly i couldnt think this way:

lods ; loads single byte from esi to al
...
sub al,'0' ; which is 30hex (ascii 30 = 0 || 39 = 9)
cmp ... ; if result is less than Ah (10d)
then store it (first shift of ebx is not counted because bl is 0)
EDIT: forgot to mention that shl x,4 will shift 4 bits which means a nibble
where you can store 0x0F in 1 nibble (4bits)
otherwise subtract 7 ; because:

ascii numbers starts from 30 to 39 hex (0-9)
and ascii chars start from 41, the distance between 39h and 41h is 7
...
loop back til al is 0 (null terminated string)

wow, you are genius .. (im bad in math sorry)
whats great in this, it doesnt matter if its a or A, f or F.


Nice job Ali.A.
lodsb also advances the esi pointer, that's why you don't have to add an extra: add esi,1.

Quote:

Q. is it important to clear DF?
anyhow bunch of thanks man.

revolution gave the answer for this.
I guess i gained some good habits from the forum. Smile
Post 02 Jul 2018, 08:42
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 719
Ali.Z 09 Apr 2019, 22:06
back to this old thread, but with something different to ask.

previously you guys helped me to convert ascii to int and int to ascii both in hex form.

now i want to convert int to ascii in decimal form, BUT without fixed point aritmetic nor div or mul instructions.

is that doable?

(again i dont know anything related to math, the only thing im good at is basic addition and subtraction lol) ... (even in real life)

_________________
Asm For Wise Humans
Post 09 Apr 2019, 22:06
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 10 Apr 2019, 02:28
Division can be emulated by using repeated subtraction. Multiplication can be emulated by repeated addition.

4 * 5 ===> 5 + 5 + 5 + 5

17 / 3 ===> 17 - 3 - 3 - 3 - 3 - 3

The basic operation for decimal conversion is division by powers of 10. So you can make code to go the "long way around" and just keep subtracting powers of 10 instead of using DIV (or the equivalent MUL by 1/10).
Post 10 Apr 2019, 02:28
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 719
Ali.Z 10 Apr 2019, 12:09
so are you saying:

Code:
; target: convert register contents into decimal form to store as ascii decimal
; edx = 00001388 ; 5000 decimal

convert:
mov eax,edx

.loop:
sub eax,0Ah
cmp eax,0Ah
jnb .loop
or eax,30h
stosb
shr edx,4
or edx,edx
jnz convert
; ...    


but that not gonna work.

_________________
Asm For Wise Humans
Post 10 Apr 2019, 12:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 10 Apr 2019, 12:29
There are two main approaches.

1) work from the MSD down to the LSD, or
2) work from the LSD up to the MSD

Let's work through approach 1
Code:
A = input number ;(5000)
call print_decimal
exit

print_decimal:
B = 1000 ;(the MSD)
call divide
display C
B = 100
call divide
display C
B = 10
call divide
display C
B = 1
call divide
display C
return

divide:
C = 0
loop:
A = A - B
if A < 0 then
 A = A + B
 return C
else
 C = C + 1
 goto loop    
Post 10 Apr 2019, 12:29
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 719
Ali.Z 10 Apr 2019, 16:18
its working, thank you.

Code:
    itoa:
              push      ebx edi
              mov       edx,[data] ; 5000
              mov       edi,[coords] ; buffer

              mov       ebx,3E8h ; 1000
              call      .convert

              mov       ebx,64h ; 100
              call      .convert

              mov       ebx,0Ah ; 10
              call      .convert

              mov       ebx,1 ; 1
              call      .convert

              pop       edi ebx
              ret
    .convert:
              or        ecx,-1
    .loop:
              sub       edx,ebx
              jns       .greater_than
              add       edx,ebx
              not       ecx
              mov       eax,ecx
              add       eax,30h
              stosb
              ret
    .greater_than:
              loop      .loop     


the problem is:
i have to add more entries, for example mov ebx,2710h (10000 decimal) if i want to convert numbers above 9999.

but for being time, its okay. (and mouse coordinates wont go above 9999 - probably)

_________________
Asm For Wise Humans
Post 10 Apr 2019, 16:18
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 10 Apr 2019, 17:28
Note that you can use decimal numbers directly in the source code:
Code:
mov ebx,1000 ;decimal 1000    
Post 10 Apr 2019, 17:28
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.