Understand assembly code in c

ZLW

I'm reading some C code embedded with a few assembly code. I understand that __asm__ is a statement to run assembly code, but what does __asm__ do in the following code? According to the output (i.e., r = 16), it seems that __asm__ does not effect the variable r. Isn't it?

#include <stdio.h>
static void foo()
{
    static volatile unsigned int r __asm__ ("0x0019");
    r |= 1 << 4;

    printf("foo: %u\n", r);
}

Platform: Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn) on OSX Yosemite

FoggyDay

Strictly speaking, your "asm" snippet simply loads a constant (0x0019).

Here's a 32-bit example:

#include <stdio.h>
static void foo()
{
    static volatile unsigned int r __asm__ ("0x0019");
    static volatile unsigned int s __asm__ ("0x1122");
    static volatile unsigned int t = 0x3344;
    printf("foo: %u %u %u\n", r, s, t);
}

gcc -O0 -S x.c

cat x.c
        .file   "x.c"
        .data
        .align 4
        .type   t.1781, @object
        .size   t.1781, 4
t.1781:
        .long   13124  # Note: 13124 decimal == 0x3344 hex
        .local  0x1122
        .comm   0x1122,4,4
        .local  0x0019
        .comm   0x0019,4,4
        .section        .rodata
.LC0:
        .string "foo: %u %u %u\n"
        .text
        .type   foo, @function
foo:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $24, %esp
        movl    t.1781, %eax
        movl    0x1122, %edx
        movl    0x0019, %ecx
        movl    %eax, 12(%esp)
        movl    %edx, 8(%esp)
        movl    %ecx, 4(%esp)
        movl    $.LC0, (%esp)
        call    printf
        leave
        ret

PS: The "asm" syntax is applicable to all gcc-based compilers.

PPS: I absolutely encourage you to experiment with assembly anywhere you please: embedded systems, Ubuntu, Mac OSX - whatever pleases you.

Here is an excellent book. It's about Linux, but it's also very largely applicable to your OSX:

Programming from the Ground Up, Jonathan Bartlett

Also:

https://www.hackerschool.com/blog/7-understanding-c-by-learning-assembly

http://fabiensanglard.net/macosxassembly/

PPS: x86 assembly syntax comes in two variants: "Intel" and "ATT" syntax. Gcc uses ATT. The ATT syntax is also applicable for any other architecture supported by GCC (MIPS, PPC, etc etc). I encourage you to start off with ATT syntax ("gcc/gas"), rather than Intel ("nasm").

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Bad Instruction - Inline Assembly Language in C Code

From Dev

Not able to understand Obfuscated C code

From Dev

C code calling assembly: infinite loop

From Dev

How to understand this embedded assembly code?

From Dev

Can someone help me understand stmdb, ldmia, and how I can go about implementing this C++ code in arm assembly language?

From Dev

Assembly Code to C

From Dev

Does C style casting adds assembly (code) or is it only for the compiler to understand the situation?

From Dev

Assembly to C Code jumps

From Dev

Reverse engineer assembly code to c code

From Dev

Try to understand calling process in assembly code

From Dev

Assembly code to C practice

From Dev

Finding missing C code, given assembly code?

From Dev

Going from Assembly to C code

From Dev

I don't understand these assembly code and machine code differences if assembly code instructions are equivalent of machine code instructions

From Dev

Assembly Compared With C code

From Dev

MIPS - Call C function in Assembly code

From Dev

How to understand this embedded assembly code?

From Dev

Not able to understand Obfuscated C code

From Dev

Understand Assembly Code

From Dev

debugging the assembly equivalent of a c code to understand the function call

From Dev

Trouble understand cbw in assembly

From Dev

Understand the output of a C code with pointers

From Dev

C loop code from assembly

From Dev

I need to understand this C code

From Dev

I try to understand [c code -> assembly] code

From Dev

Assembly code to C practice

From Dev

Trying to convert this Assembly code to C code

From Dev

How to understand this TMG's assembly code from the Version 6 Unix?

From Dev

unable to understand the base pointer calculation in assembly code

Related Related

  1. 1

    Bad Instruction - Inline Assembly Language in C Code

  2. 2

    Not able to understand Obfuscated C code

  3. 3

    C code calling assembly: infinite loop

  4. 4

    How to understand this embedded assembly code?

  5. 5

    Can someone help me understand stmdb, ldmia, and how I can go about implementing this C++ code in arm assembly language?

  6. 6

    Assembly Code to C

  7. 7

    Does C style casting adds assembly (code) or is it only for the compiler to understand the situation?

  8. 8

    Assembly to C Code jumps

  9. 9

    Reverse engineer assembly code to c code

  10. 10

    Try to understand calling process in assembly code

  11. 11

    Assembly code to C practice

  12. 12

    Finding missing C code, given assembly code?

  13. 13

    Going from Assembly to C code

  14. 14

    I don't understand these assembly code and machine code differences if assembly code instructions are equivalent of machine code instructions

  15. 15

    Assembly Compared With C code

  16. 16

    MIPS - Call C function in Assembly code

  17. 17

    How to understand this embedded assembly code?

  18. 18

    Not able to understand Obfuscated C code

  19. 19

    Understand Assembly Code

  20. 20

    debugging the assembly equivalent of a c code to understand the function call

  21. 21

    Trouble understand cbw in assembly

  22. 22

    Understand the output of a C code with pointers

  23. 23

    C loop code from assembly

  24. 24

    I need to understand this C code

  25. 25

    I try to understand [c code -> assembly] code

  26. 26

    Assembly code to C practice

  27. 27

    Trying to convert this Assembly code to C code

  28. 28

    How to understand this TMG's assembly code from the Version 6 Unix?

  29. 29

    unable to understand the base pointer calculation in assembly code

HotTag

Archive