flat assembler
Message board for the users of flat assembler.

Index > Main > Increment text value?

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 815
Roman
* - this mean space(32)
I have text value txtVal db '1***'
Next i increment txtVal and get txtVal db '2***'
Do until txtVal db '9 '
Next increment get txtVal db '10**'
Next increment get txtVal db '11**'

I need increment to 9000 how do this more short and more right and more beautiful ?
Post 08 Aug 2020, 11:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17662
Location: In your JS exploiting you and your system
revolution
Backwards scan through the buffer to find a digit and increment it.
If it overflows then go back one and increment it.
If it overflows then go back one and increment it.
...
If you reach the beginning of the buffer then place a '1' and follow it with '0's up to the first '*'

BUT I think a better method is to use a binary counter and convert it to ASCII into the buffer.
Post 08 Aug 2020, 11:32
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 815
Roman
I try it but always get 00001
Code:
num1 db '00001'
num2 db '00000'
sum  db '     ',0
start:
    mov edi,111
ad_ll:
   mov     esi, 4       ;pointing to the rightmost digit
   mov     ecx, 5       ;num of digits
   clc
add_loop:  
   mov  al, [num1 + esi]
   adc  al, [num2 + esi]
   aaa
   ;pushf
   or   al, 30h
   ;popf
        
   mov  [sum + esi], al
   dec  esi
   dec  ecx
   test ecx,ecx
   jnz  add_loop
   ;Msg sum,0
   mov eax,dword [sum]
   mov dword [num2],eax
   mov al,[sum+4]
   mov [num2+4],al
   dec edi
   test edi,edi
   jnz ad_ll         
    
Post 08 Aug 2020, 13:37
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17662
Location: In your JS exploiting you and your system
revolution
You appear to have changed your question to adding two ASCII encoded text values. Or did you post the wrong code?
Post 08 Aug 2020, 13:42
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 815
Roman
This code give correct result if
num1 db '02401'
num2 db '00602'

I get 3003

And I think do trick. Increase num2
Post 08 Aug 2020, 13:53
View user's profile Send private message Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 60
Location: Belgium
SeproMan
Your program needs to preserve the carry flag so that adc can do its job. The or al, 30h instruction will zero the carry flag and I see that you've tried surrounding it with push/pop. That's fine but the test ecx,ecx instruction down the loop also zeroes the carry flag! Luckily you don't need that test because the dec ecx instruction already provides the necessary zero flag info.

This is the revised code:

Code:
num1 db '00001'
num2 db '00000'
sum  db '     ',0
start:
    mov edi,111
ad_ll:
   mov     esi, 4       ;pointing to the rightmost digit
   mov     ecx, 5       ;num of digits
   clc
add_loop:  
   mov  al, [num1 + esi]
   adc  al, [num2 + esi]
   aaa
   pushf
   or   al, 30h
   popf
        
   mov  [sum + esi], al
   dec  esi
   dec  ecx             ; No more test ecx,ecx here!
   jnz  add_loop
   ;Msg sum,0
   mov eax,dword [sum]
   mov dword [num2],eax
   mov al,[sum+4]
   mov [num2+4],al
   dec edi
   test edi,edi          ; You can drop this too!
   jnz ad_ll    


And this is a somewhat shorter version. I no longer use a separate loop counter in the ECX register, but rely on the index in the ESI register becoming negative:

Code:
num1 db '00001'
num2 db '00000'
sum  db '     ',0
start:
   mov  edi,111
ad_ll:
   mov  esi, 4       ;pointing to the rightmost digit
   clc
add_loop:  
   mov  al, [num1 + esi]
   adc  al, [num2 + esi]
   aaa
   pushf
   or   al, 30h
   popf
   mov  [sum + esi], al
   dec  esi
   jns  add_loop

   mov  eax, [sum]
   mov  [num2], eax
   mov  al, [sum+4]
   mov  [num2+4], al
   dec  edi
   jnz  ad_ll    

_________________
Real Address Mode.
Post 09 Aug 2020, 15:34
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 815
Roman
SeproMan your example get me 00111 !
Thanks !
Post 09 Aug 2020, 17:26
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.