I'm trying to calculate the value of sum of two linked list each representing a number. Every link in a list contains 5 bytes. The first byte representing the value of the link (4 bits for representing an hexa digit and 4 zeroes) and the other 4 bytes is the address of the next link in the list. Also I allocated bytes called "stack" holds the pointers to the first links of every linked list. The first link in every list is the less significant digit of the number it is represented. I'm trying to calculate the value on the top two lists in my stack. I iterate them both and considerate the carry, then every iterate push the result to the stack and eventually pop the results from the stack and create new list with the value of the sum.
Code snipped:
my auxiliary functions:
section .data
firstLink: dd 0
stackSize: dd 0
stackCurSizeBytes: dd 0
stackPointer: dd 0
stackCurSize: dd 0
nextLink: dd 0
data: db 0
carry: db 0
numPush: dd 0
%macro addNewLink 1
mov [add], %1
push 5
call malloc
add esp, 4
mov bl, [add]
mov [eax], byte bl
mov edx, [firstLink]
mov [eax+1], edx
mov [firstLink], eax
mov [add], byte 0
%endmacro
%macro getNext 1 ; get pointer to link and return pointer to next link
mov eax, %1
mov eax, [eax+1]
mov [nextLink], eax
%endmacro
%macro pushLinkedList 0
mov edx, [firstLink]
mov ecx, [stackPointer]
add ecx, [stackCurSizeBytes]
mov [ecx], edx
inc dword [stackCurSize]
add [stackCurSizeBytes], dword 4
%endmacro
%macro getLinkData 1 ; gets pointer to link and return its data (byte)
mov ebx, %1
mov ebx, [ebx]
mov [data], dword ebx
%endmacro
sum function:
ratorPlus:
cmp [stackCurSize], dword 1
jle argumentError
mov edx, [stackPointer] ;edx has the top operand pointer
add edx, [stackCurSizeBytes]
sub edx, 4
mov edx, [edx]
dec dword [stackCurSize] ; dec stack
sub [stackCurSizeBytes], dword 4 ; dec stack
mov ecx, [stackPointer] ;ecx has the second top operand pointer
add ecx, [stackCurSizeBytes]
sub ecx, 4
mov ecx, [ecx]
dec dword [stackCurSize] ; dec stack
sub [stackCurSizeBytes], dword 4 ; dec stack
mov [firstLink], dword 0 ;initialize function params
mov [numPush], dword 0
mov [carry], byte 0
checkLen3:
cmp edx, dword 0x00
je endFirstNumber3
cmp ecx, dword 0x00
je SecondEndedFirstNot3
;both numbers still not null
getLinkData edx
mov ebx, [data] ;ebx is the first list data
mov [tempreg], dword ebx
getLinkData ecx
mov ebx, [tempreg]
mov eax, [data] ;eax is the second list data
add ebx, eax ;preform plus and and stores the result in ebx
add ebx, [carry]
cmp ebx, 0xF
jle noCary1
mov [carry], byte 1
sub ebx, 0x10
jmp endCary1
noCary1:
mov [carry], byte 0
jmp endCary1
endCary1:
push ebx
inc dword [numPush]
getNext edx
mov edx, [nextLink]
getNext ecx
mov ecx, [nextLink]
jmp checkLen3
endFirstNumber3:
cmp ecx, dword 0x00
je BothEnded3
;second is longer
getLinkData ecx
mov eax, [data] ;eax is the data
add eax, [carry]
cmp eax, 0xF
jle noCary2
mov [carry], byte 1
sub eax, 0x10
jmp endCary2
noCary2:
mov [carry], byte 0
jmp endCary2
endCary2:
push eax
inc dword [numPush]
getNext ecx
mov ecx, [nextLink]
jmp checkLen3
SecondEndedFirstNot3: ;first s longer
getLinkData edx
mov eax, [data] ;eax is the data
add eax, [carry]
cmp eax, 0xF
jle noCary3
mov [carry], byte 1
sub eax, 0x10
jmp endCary3
noCary3:
mov [carry], byte 0
jmp endCary3
endCary3:
push eax
inc dword [numPush]
getNext edx
mov edx, [nextLink]
jmp checkLen3
BothEnded3:
cmp [carry], byte 1 ;both numbers ended
jne BothEnded2Loop
push 1
inc dword [numPush]
BothEnded2Loop:
cmp [numPush], dword 0
je endRatorPlus
pop eax
addNewLink al
dec dword [numPush]
jmp BothEnded2Loop
endRatorPlus:
mov [numPush], dword 0
mov [carry], dword 0
pushLinkedList
jmp running
I think the logic here is pretty simple but somehow it doesn't work well, I also tried to debug it using gdb but then wierd things happens.