Hi guys.
I have to understand how this program works. All comments are mine. Could someone make me sure my thoughts are valid? Thisprogram count 128 bit factorial. Why there are not any mul instructions? All additional comments are welcome : )
;128 bits is too big for mul instruction,
;which saves only 64-bits result (EDX (32bits) + EAX (32bits)).
;So first of all you need 4 DWORDS of memory to store 128bits result.
;Then you should replace your mul ebx; on some procedure call,
;that can handle 128bits multiplication.
SECTION .data
formatd: db "%d", 0 ; integer??
formatx: db "%08x", 0 :??
name: resb 20 ;reserve 20 bytes, they declare uninitialized storage space.
number: resb 4
array resd 4
backup resd 4
SECTION .text
extern printf
extern scanf
global main
main:
push EBP
mov EBP, ESP ;base pointer, stack pointer
push number
push formatd
call scanf
add ESP, 8 ; all: read number
mov ebx, dword [number]
mov dword [array+0], 0x0 ;initialize with zeros
mov dword [array+4], 0x0
mov dword [array+8], 0x0
mov dword [array+16], 0x0
mov dword [backup+0], 0x0
mov dword [backup+4], 0x0
mov dword [backup+8], 0x0
mov dword [backup+16], 0x0
mov [array+0], eax
outer_loop:
cmp ebx, 0x1
je end
mov eax, [array+0] ;assignment a numbers to the array
mov [backup+0], eax
mov eax, [array+4]
mov [backup+4], eax
mov eax, [array+8]
mov [backup+8], eax
mov eax, [array+12]
mov [backup+12], eax
mov ecx, ebx
dec ecx
inner_loop:
clc
; adding 128 bit number
mov eax, [backup+0]
adc [array+0], eax ;add carried part from previous multiplication
mov eax, [backup+4]
adc [array+4], eax
mov eax, [backup+8]
adc [array+8], eax
mov eax, [backup+12]
adc [array+12], eax
clc ;Clear carry flag
loop inner_loop ; uses ecx (or cx in 16-bit code)
dec ebx
jmp outer_loop
end:
push dword [array+12] ; printing number
push formatx
call printf
add ESP, 8
push dword [array+8]
push formatx
call printf
add ESP, 8
push dword [array+4]
push formatx
call printf
add ESP, 8
push dword [array+0]
push formatx
call printf
add ESP, 8
mov ESP, EBP
pop EBP
ret