flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
revolution 08 Aug 2020, 11:32
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. |
|||
![]() |
|
Roman 08 Aug 2020, 13:37
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 |
|||
![]() |
|
revolution 08 Aug 2020, 13:42
You appear to have changed your question to adding two ASCII encoded text values. Or did you post the wrong code?
|
|||
![]() |
|
Roman 08 Aug 2020, 13:53
This code give correct result if
num1 db '02401' num2 db '00602' I get 3003 And I think do trick. Increase num2 |
|||
![]() |
|
SeproMan 09 Aug 2020, 15:34
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. |
|||
![]() |
|
Roman 09 Aug 2020, 17:26
SeproMan your example get me 00111 !
Thanks ! |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.