0

I am writing power function in GNU Assembly x86 on Linux principal-neanderthal 6.8.0-41-generic x86_x64. When i launch i get "segmetantion fault (core dumped)" error.

.code32
.section .data

.section .text

.globl _start
_start:
  pushl $3
  pushl $2
  call power
  addl $8, %esp
  pushl %eax

  pushl $2
  pushl $5
  call power
  addl $8, %esp

  popl %ebx
  addl %eax, %ebx

  movl $1, %eax
  int $0x80

.type power, @function
power:
  pushl %ebp
  movl %esp, %ebp
  subl $4, %esp
  movl 8(%ebp), %ebx                   # HERE IS THE PROBLEM 
  movl 12(%ebp), %ecx
  movl %ebx, -4(%ebp)

power_loop_start:
  cmpl $1, %ecx
  je end_power
  movl -4(%ebp), %eax
  imull %ebx, %eax
  movl %eax, -4(%ebp) 
  decl %ecx
  jmp power_loop_start

end_power:
  movl -4(%ebp), %eax
  movl %ebp, %esp 
  popl %ebp 
  ret

After debugging in gbd found out that something is wrong in power function at 31 line when i try to load first argument in %ebx register. Also in gdb view %ebp, %eax, %ebx registers are replaced by their x64 variants with r suffix. Would be very thankful for explaining.

6
  • 5
    How do you create the program? Have you used the proper options to produce 32 bit binary? Using gcc -m32 -nostdlib it works here and produces exit code 33 which I believe is the expected result. Commented Sep 10, 2024 at 17:26
  • 2
    @FischerBenkovscki Your program is written for 32-bit x86 mode but you assembled and run it as 64-bit x86_64. Commented Sep 11, 2024 at 10:00
  • 1
    If you want to use as and ld then do as --32 and ld -melf_i386 Commented Sep 11, 2024 at 10:52
  • 1
    @dimich Tell tale sign is the .code32 directive without which it wouldn't assemble with a 64 bit assembler. OP: do not specify this directive. If it seems to be required, you're using the wrong assembler! It merely masks the problem, but doesn't solve it. Use a 32 bit assembler for 32 bit code. Commented Sep 12, 2024 at 9:56
  • 1
    @Jester thank you very much. I was thinking that .code32 in the beginning is enough. You really helped me Commented Sep 13, 2024 at 17:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.