I write a program that does division and mod operations with a long int and an int array. When I want to write my division operation's result but I can't. I can't figure out the problem, but I think I used the register assignments process from the data section wrong. Please help me. I think I call array and int array wrong from the data section or I have problems with registers using. This is my code:
global _start
section .text
_start:
mov r9, number ; rdi stores adress of number array
mov r12, 0 ; get number of k for determine index of number array
L3: ; function begin
mov r15, 0 ; get number of i because using i of loop register
mov r14, [digits] ; get number of digits
mov r13, [r9 + r12*4] ; get array number for using array adress. We use 4*k because we take k. index of array
syscall
cmp r13, 0 ; determine number is negative or positive
jl L2 ; if it is negative we goto else part for print minus
; print blank otherwise we print blank
mov rax, 1
mov rdi, 1
mov rsi, blank
mov rdx, 1
syscall
jmp L1 ; after printing, jump L1 statement
L2: ; if number is negative continiuos here so Else part
; print minus
mov rax, 1
mov rdi, 1
mov rsi, minus
mov rdx, 1
syscall
neg r13 ; convert the negative number to positive number
L1: ; into the loop
; division operation digit = (number/digits)
mov rax, r13 ; dividend
cqo
mov rbx, r14 ; divisor
idiv rbx ; after the div operation rbx store division
mov r10, rax ; result is egual digit (r10)
; print digit
add rsp , 8
add r10, 48
mov rax , 1
mov rdi, 1
push r10
mov rsi, rsp
mov rdx, 1
syscall
; mod operation number = number % digits
mov rax, r13 ; dividend
cqo
mov rbx, r14 ; divisor
xor rdx, rdx ; rdx which store reminder be zero
idiv rbx ; after the div operation rbx store division
mov r13, rdx ; rdx store reminder and reminder egual number
; division operation digits = (digits/10)
mov rdx, 1
mov rax, r14 ; dividend
mov rbx, 10 ; divisor
div rbx ; after the div operation rbx store division
mov r14, rbx ; result is egual digit (r14)
inc r15 ; increment the loop register
cmp r15, 9 ; compare loop register with 9 so loop work 8 times
jl L1 ; if loop register less than 9 return loop
; print new line
mov rax, 1
mov rdi, 1
mov rsi, newline
mov rdx, 1
syscall
inc r12 ; increment k by 1
cmp r12, 8 ; compare k with numberCount
jl L3 ; if k less than numberCount, goto L3 that is begining write9digits function
Exit: ; if array index(k) greater than numberCount work Exit
mov rax, 60 ; system call for exit
xor rdi, rdi ; exit code 0
syscall ; invoke operating system to exit
section .data
newline db 10 ; note the newline at the end
blank db ' '
minus db '-'
i dd 0
k dd 0
digits dd 100000000
number dd 321762410, 9, 2943018, 0, 19372039, 18, -76241, -208424
this code will print this
00000000
0�0000000
0�0000000
0�0000000
000000000
-0�0000000
-0�0000000
0_0000000
but Acorrding to my c code result must be this:
321762410
000000009
002943018
000000000
019371039
000000018
-000076241
-000208424
I tried to use qword to convert take integer but nothing changed. After I changed the integers' format to hexadecimal format, nothing changed. Finally, I tried to change [digits] and [r9 + r12*4].
mov r14, [digits] ; get number of digits
mov r13, [r9 + r12*4] ; get array number for using array adress. We use 4*k
if I changed the [digits] as 5 and [r9 + r12*4] as 21, I take this output:
mov r14, 5 ; get number of digits
mov r13, 21 ; get array number for using array adress. We use 4*k
400000000
400000000
400000000
400000000
400000000
400000000
400000000
400000000