mrQWERTY
mrQWERTY

Reputation: 4149

x86 Assembly program does not terminate

I am learning assembly and my task was to convert a simple program from 32 bit to 64 bit. The program computes 2^3 + 5^2. My program compiles fine, however, when I run it the program does not terminate. Therefore, I have to Ctrl + C in the command line to actually stop the program. I was thinking that the problem initially was because I used int 0x80 to terminate, but after I switched to syscall the problem persisted.

The code:

  1 .section .data
  2 .section .text
  3 .globl _start
  4 _start:
  5         push $3
  6         push $2
  7 
  8         call power
  9         addq $8, %rsp
 10 
 11         push %rax
 12 
 13         push $2
 14         push $5
 15 
 16         call power
 17         addq $8, %rsp
 18         pop %rbx
 19 
 20         addq %rax, %rbx
 21 
 22         movq $1, %rax
 23         syscall
 24 
 25 .type power, @function
 26 power:
 27         push %rbp
 28         movq %rsp, %rbp
 29         subq $4, %rsp
 30 
 31         movq 8(%rbp), %rbx
 32         movq 12(%rbp), %rcx
 33 
 34         movq %rbx, -4(%rbp)
 35 
 36 power_loop_start:
 37         cmpq $1, %rcx
 38         je end_power
 39 
 40         movq -4(%rbp), %rax
 41         imulq %rbx, %rax
 42         movq %rax, -3(%rbp)
 43         decq %rcx
 44         jmp power_loop_start
 45 
 46 end_power:
 47         movq -4(%rbp), %rax
 48         movq %rbp, %rsp
 49         pop %rbp
 50         ret

I'm sorry for posting this much code, but I need guidance as I am totally clueless. I've searched for a long time with no avail. Thanks in advance. BTW, the program works before converting it to 64 bit. I simply changed all movl etc to movq and used 64 bit registers.

Upvotes: 2

Views: 509

Answers (1)

Tyler
Tyler

Reputation: 1827

It looks like you're using 1 as the system call number, but it seems that it is actually 60. Apparently, you can't assume the syscall numbers aren't the same from 32bit linux to 64bit.

Upon further inspection, it seems there is a few more things wrong.

  • You don't pass args on the stack in 64bit (unless you have so many they won't fit in the given regs. Look up the linux 64bit calling convention)
  • In main, you push 2 dwords, but in power you get 2 qwords off the stack.

All the places you use movq to access memory are likely not working as expected, because the accesses are meant for dwords. You're reading or writing the higher 32bits of the number without meaning to.

Upvotes: 1

Related Questions