; Imports:
; Exports:
global _start
section .code code
strlen:
; Params:
; RSI = string
; Returns:
; EAX = length
; Beginning:
push rbx
push rsi
; Find the string's length:
; Header:
xor eax, eax
xor ebx, ebx
mov rsi, char_buffer
; Loop body:
.loop1:
inc ebx
lodsb
test eax, eax
jz .loop1
mov eax, ebx
dec eax
dec eax
; End:
pop rsi
pop rbx
ret
_start:
; Align stack:
;push rax
; Get numbers:
mov ecx, 3
mov rdi, value1
mov r15, char_buffer ; Save a bit of space.
; Loop body:
.loop1:
push rcx
xor eax, eax
xor edi, edi
mov rsi, r15
mov rdi, 12
syscall
; Convert the string to u32:
; Find string length:
mov rsi, r15
;call strlen
; Go through the characters to convert them to an integer:
mov ecx, eax
mov rsi, r15
add rsi, r14
dec rsi
mov r13d, 1
xor ebx, ebx
std
.loop1_2:
; dec r14d
; xor eax, eax
; lodsd
; sub al, '0'
; imul eax, r13d
; add ebx, eax
; imul r13d, r13d, 10
; loop .loop1_2
cld
; Save the number:
; mov eax, ebx
; stosd
pop rcx
loop .loop1
; Calculate the value:
; Get the smaller candy price:
; mov eax, [rel value3]
; mov ebx, [rel value2]
; cmp ebx, eax
; cmovg ebx, eax ; Assumption: All x64 CPUs support CMOV.
; Divide the amount of money by the candy price:
; xor edx, edx
; mov eax, [rel value1]
; div ebx
; Convert EDX to the value to output:
;sub rsp, 32
;mov ecx, eax ; value
;mov rdx, char_buffer ; buffer
;mov r8d, 12 ; size
;mov r9d, 10 ; radix
;call _itoa_s
;add rsp, 32
; Output the number:
mov rsi, r15
;call strlen
mov edx, eax
mov eax, 1
xor edi, edi
mov rsi, char_buffer
syscall
; Exit the process:
mov eax, 60
xor edi, edi
syscall
; Static allocations:
section .bss bss
string_lenght: resd 1
value1: resd 1
value2: resd 1
value3: resd 1
char_buffer: resb 12