+ Reply to Thread
Results 1 to 4 of 4

Thread: Why does asm("mov 0x12345678, %ax") cause a seg fault?

  1. #1
    Newbie mynameisflorian is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    2

    Why does asm("mov 0x12345678, %ax") cause a seg fault?

    Why does asm("mov 0x12345678, %ax") cause a seg fault?

    I'm tinkering with assembly and can't understand why this is happening. I hope someone can explain it for me.

    I have a simple c++ program. I added a line with asm("mov 0x12345678, %ax") so I can find that part in the assembly code.

    If I compile then run it with gdb, it encounters a seg fault at XYZ. If I "objdump -d a.out > out.s" and look for XYZ -- guess what? that's the line where my mov statement is, not after. I don't think the following instruction would be affected by this ("movq -32(%rbp), %rax"), but it's not faulting on that line, it's faulting on the 0x12345678 line. I've tried %rax, %eax, %bx, %rbx, etc -- all fault. BTW, this is a 64-bit linux target...

    Any Ideas?

    - Florian

  2. #2
    The Crazy One TkTech will become famous soon enough TkTech's Avatar
    Join Date
    Jun 2006
    Location
    Canada
    Age
    18
    Posts
    1,549
    Blog Entries
    1

    Re: Why does asm("mov 0x12345678, %ax") cause a seg fault?

    A. You need to prefix literal values with '$'. You aren't trying to move 0x12345678 into %ax, you're moving 16bits from the address 0x12345678 into %ax. Hello, seg fault.

    B. $0x12345678 is a 32bit value. You're trying to move it into a 16bit register, %ax.

  3. #3
    Newbie mynameisflorian is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    2

    Re: Why does asm("mov 0x12345678, %ax") cause a seg fault?

    A) uh ... duh ... thanks!

    B) sorry, I started off with %eax. %ax must've snuck in there during the "try random stuff until it works" phase (that's the one right before I give up and ask for help)

    Again, thanks for your help!

    - Florian

  4. #4
    Newbie foxmuldr is an unknown quantity at this point
    Join Date
    Dec 2009
    Posts
    5

    Re: Why does asm("mov 0x12345678, %ax") cause a seg fault?

    Non-Intel syntax always confuses me.

    BTW, if you're looking for a way to inject assembly code into your program, you can use "asm("int 3")" and "asm("nop")" as one-byte opcodes which will cause a break in your program in all conditions, and can then be replaced with edit-and-continue (or by writing the byte 0x90 to the memory address) to the NOP version, which doesn't do anything, but is simply an opcode placeholder. Many debuggers do this.

    The int 3 opcode can be either 0xcc, or 0xcd, 0x03, and both work identically, it's just that one of them is one-byte, the other is two.

    - Rick

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. protected mode
    By h4x in forum Assembly
    Replies: 25
    Last Post: 09-27-2009, 11:34 PM
  2. Replies: 1
    Last Post: 01-21-2008, 04:43 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts