kww228 0 Newbie Poster

I am trying to make a program that will do addition and subtraction on positive integers of any length represented in characters using ASCII operations. The code I have work for the addition but the subtraction brakes around line 35. Thank you for the help. Code is listed below

include irvine32.inc
.data
x   byte   '999999999999'
y   byte   '123456789012'
z   byte   20 dup (' ');

distance = sizeof x

.code
begin:

mov esi,(offset x) + distance - 1               ;esi -> rightmost of x
mov ecx,distance                                ;ecx = # of additions
mov edi,(offset z) + sizeof z - 1               ;edi -> rightmost of z
clc 
                                        ;clear CF
nextAdd:
mov al,[esi]                                    ;al = a digit from x
adc al,[esi+distance]                           ;al = x + y + CF
aaa                                             ;adjust sum in al
pushf                                           ;preserve the lower 16 bits (CF is among them) of EFLAGS 
add al,30h                                      ;convert the numberic sum back to ascii
mov [edi],al                                    ;save the ascii sum in z
dec esi                                         ;prepare for the next add on the left
dec edi
popf
loop nextAdd

nextSub:
mov al,[esi]                                    ;al = a digit from x
adc al,[esi+distance]                           ;al = x + y + CF
aas                                             ;adjust diffance in al
pushf                                           ;preserve the lower 16 bits (CF is among them) of EFLAGS 
sub al,30h                                      ;convert the numberic diffance back to ascii
mov [edi],al                                    ;save the ascii diffance in z
dec esi                                         ;prepare for the next subtract on the left
dec edi
popf
loop nextSub

jc  carryOut                                    ;there is a carry out of the most significant bit
jmp fin

carryOut:
mov byte ptr [edi], '1'

fin:
invoke exitProcess,0
end begin
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.