SeigNeo
SeigNeo

Reputation: 41

Understanding assembly from a gcc output

I'm doing a high school course about computer fundamentals and I'm trying to understand assembly code and created a hello world in C and compiled it in assembly code. I understand that "mov r0,r3" moves data from register 3 to register 0. However, how do I figure out what what the value of r3 is?

Below is the assembly code that I'm using for my understanding from:

.arch armv6
.eabi_attribute 27, 3
.eabi_attribute 28, 1
.fpu vfp
.eabi_attribute 20, 1
.eabi_attribute 21, 1
.eabi_attribute 23, 3
.eabi_attribute 24, 1
.eabi_attribute 25, 1
.eabi_attribute 26, 2
.eabi_attribute 30, 6
.eabi_attribute 34, 1
.eabi_attribute 18, 4
.file   "hello.c"
.section    .rodata
.align  2
.LC0:
    .ascii  "Hello World\000"
    .text
    .align  2
    .global main
    .type   main, %function
main:
    @ args = 0, pretend = 0, frame = 0
    @ frame_needed = 1, uses_anonymous_args = 0
    stmfd   sp!, {fp, lr}
    add     fp, sp, #4
    ldr     r0, .L2
    bl      printf
    mov     r0, r3
    ldmfd   sp!, {fp, pc}
.L3:
    .align  2
.L2:
    .word   .LC0
    .size   main, .-main
    .ident  "GCC: (Raspbian 4.9.2-10) 4.9.2"
    .section    .note.GNU-stack,"",%progbits

Below is my C code:

// Hello World program in C

#include<stdio.h>

main()
{
    printf("Hello World");
}

Upvotes: 4

Views: 1460

Answers (2)

Scott Norton
Scott Norton

Reputation: 39

Well, let's break it down.

stmfd sp!, {fp, lr}

STMFD means "STore Multiple Full Descending". Essentially, it's using the register sp as a descending stack pointer and placing the registers fp and lr on the stack, in that order. fp is the frame pointer, lr is the link register (holds the return address, important later).

add fp, sp, #4

Sets the frame pointer register to be equal to sp+4 (points to where fp was just stored). This is an artifact of compilation without optimization.

ldr r0, .L2

This is shorthand for loading the 32-bit value stored at address .L2 (which in this case is the address of .LC0, the string (well, char array) "Hello World!\000") into register r0.

bl printf

Assembled by calling the printf function on the null-terminated string "Hello World". Recall that the pointer to this array was just loaded into r0. In general, GCC uses registers r0-r3 as the first, second, third, and fourth arguments to functions, respectively. Additional arguments are stored on the stack as needed.

mov r0, r3

This is an indicator of a bug in your code, which this version of GCC did not catch. Basically, your function returns a type (assumed to be int because you never specified otherwise), but you never specified a return value. Thus the return value is whatever happened to be in r3, which is undefined.

ldmfd sp!, {fp, pc}

This is the reverse of the stmfd instruction compiled at the beginning of the function. But instead of loading into fp, lr, we're loading into fp, pc. This causes the original value of lr to be moved directly into the program counter pc. pc is a special register in that it points to the next instruction executed. Any time pc is set to a value, the CPU immediately starts executing code pointed to by that value. This property is used to return to the calling routine, since the ARM instruction set does not have an explicit ret operation.

Upvotes: 1

TobTobXX
TobTobXX

Reputation: 509

Use the GNU Debugger, "gdb"!
gdb --args ./store01 starts the GNU Debugger.
It should act something like a console.
You exit by typing quit.
But of course you want to run the programm step by step and check the register contetns. So type start, which jumpes to main() and skips the initialization stuff.
Then type disassemble or disas to show the assebler you're running.
Now run the program instruction per instruction per typing stepi.
And now the interesting part: type info registers r3 and watch the output!
And another fantastic thing: you can CHANGE the values runtime: try p $r0 = 2.
That's not all.
Here's the official documentation:https://www.gnu.org/software/gdb/documentation/
And a good, small and useful tutorial: http://thinkingeek.com/2013/01/12/arm-assembler-raspberry-pi-chapter-4

Upvotes: 3

Related Questions