7

I am learning arm assembly language in my one course. I am having little problem in getting started. I have written a simple c code:

int main()
{
    int a = 10;
    int b = 20;
    int c = a+b;
}

And then I converted it to assembly code using gnu arm by giving the command:

arm-elf-gcc -S first.c

This generated a file first.s containing assembly code:

    .file   "first.c"
    .text
    .align  2
    .global main
    .type   main, %function
main:
    @ args = 0, pretend = 0, frame = 12
    @ frame_needed = 1, uses_anonymous_args = 0
    mov ip, sp
    stmfd   sp!, {fp, ip, lr, pc}
    sub fp, ip, #4
    sub sp, sp, #12
    mov r3, #10
    str r3, [fp, #-16]
    mov r3, #20
    str r3, [fp, #-20]
    ldr r2, [fp, #-16]
    ldr r3, [fp, #-20]
    add r3, r2, r3
    str r3, [fp, #-24]
    mov r0, r3
    sub sp, fp, #12
    ldmfd   sp, {fp, sp, pc}
    .size   main, .-main
    .ident  "GCC: (GNU) 3.4.3"

Then I compiled the assembly code using following command:

arm-elf-gcc -g first.s

This generated a.out binary file. Then I tried to run a.out with qemu using command:

qemu-arm a.out

But this generates output

Segmentation fault

I can't find the mistake, what am I doing wrong?

1 Answer 1

7

You are trying to run qemu in user mode. You also need to link the libraries which corresponds to arm.

take a look at the script files in below pkg.

http://wiki.qemu.org/download/linux-user-test-0.3.tar.gz

You will need to run qemu -L library_PATH_ARM ./a.out

Sign up to request clarification or add additional context in comments.

2 Comments

I used command qemu-arm -L ./gnemul/qemu-arm ./a.out but no help, it again giving same Segmentation fault. I have pasted gnemul folder from the linux-user-test folder.
Try with qemu-arm -L /usr/arm-linux-gnueabi/ ./a.out. before that you need to install arm-linux-gnueabi-gcc. And compile also using arm-linux-gnueabi-gcc. This works for me.

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.