extern io_get_dec, io_print_dec
section .bss
cntr resd 1
cur resb 1
n resd 1
k resd 1
section .text
global main
main:
call io_get_dec
mov [n], eax
call io_get_dec
mov [k], eax
active_loop:
cmp [n], 0
jng finish
mov [cur], 0
mov eax, [n]
fixed_number:
cmp eax, 0
je maybe_inc_cntr
cmp [cur], k
jg cur_became_big
and eax, 1
cmp eax, 0
je inc_cur
shr eax, 1
jmp fixed_number
inc_cur:
inc cur
shr eax, 1
jmp fixed_number
cur_became_big:
mov [cur], 0
dec [n]
jmp active_loop
maybe_inc_cntr:
cmp [cntr], k
je inc_cntr
dec [n]
jmp active_loop
inc_cntr:
inc cntr
dec [n]
jmp active_loop
finish:
mov eax, [cntr]
call io_print_dec
xor eax, eax
ret