Reputation: 129
I have this code I wrote in x86. The code does a power of the first number I'm pushing to the queue by the second one.
Now all cases work as expected except one case that's when the second number equals $1
and then for some reason the output is 0, however I didn't managed to determine why when I debugged it...
(BTW I'm using mul
instruction that one time put the output in %rax
and one time in %rdx
, so I did a logic that determines from which one to take the output each time (by counting the iterations). It seems to me that this implementation is quite bad so I would like to hear how you would deal with that).
Here's the code:
.section .data
.section .text
.globl _start
_start:
pushq $3
pushq $1
call power_load_values
jmp exit_with_solution
power_load_values:
mov %rsp,%rbp
mov 4(%rsp),%rcx
mov 4(%rsp),%rax
mov 8(%rsp),%rbx
jmp power_loop
return_to_main:
mov %rbp,%rsp
add $8,%rbp
ret
power_loop:
inc %rdx
cmp $1,%rbx
je return_to_main ;if the second number is 1 so the output is just the first one (no need to do power)
cmp $0,%rbx
je set_as_one ;power zero is one
mul %rcx
dec %rbx
test $1,%rdx ;checking if its odd or even. event - take from rax, odd - take from rdx
jnz from_rdx
jz from_rax
from_rdx:
mov %rdx,%rcx
mov 8(%rsp),%rax ;because rax is being used for 'mul' operator and it resets after calling it
jmp power_loop
from_rax:
mov %rax,%rcx
mov 8(%rsp),%rax ;because rax is being used for 'mul' operator and it resets after calling it
jmp power_loop
set_as_one:
mov $1,%rcx
jmp return_to_main
exit_with_solution:
movl $1,%eax
mov %rcx,%rbx
int $0x80
I would like to hear some advice from you guys.
Upvotes: 1
Views: 57