Converting C to Assembly – A Programmer's Tale ??

Converting C to Assembly – A Programmer's Tale ??

Hello LinkedIn Community! ??

Let me take you on a delightful detour I recently embarked upon in the world of programming. It all started quite unexpectedly, inspired by a snippet from a vlogger's tech interview experience.

The Spark: From Vlog to Venture

Picture this: a regular day turned intriguing. I was watching Piyush Itankar's vlog, where he shared a brain-teaser from his ARM interview - converting C code into assembly. A light bulb went off, and I thought, "Why not try this myself?" Imagine translating Shakespeare into Morse code – that's the level of thrill I anticipated!

The Process: C to Assembly

To demystify this process, let's take a classic example - the 'printf' function in C. We all use it, but how does it work under the hood? When we compile a C program, each function, including 'printf', is translated into a series of assembly instructions. These instructions are what the CPU directly executes.


The C to Assembly Odyssey

Diving into this challenge, I realised it's more than just a technical exercise; it's a detective mission unraveling how high-level abstractions in C translate to the low-level nitty-gritty of assembly language.

?? Example Dissection:

  1. 'Hello, World!' – The Classic Starter

C Code:

Assembly Code:

Assembly Breakdown:

  • .section __TEXT,__text: Denotes the start of the text section where code is placed.
  • _main:: Label indicating the start of the main function.
  • pushq %rbp: Saves the base pointer on the stack, a standard function prologue.
  • movq %rsp, %rbp: Sets up the base pointer for the current stack frame.
  • leaq L_.str(%rip), %rdi: Loads the effective address of the "Hello, World!" string into %rdi, the first argument for printf.
  • movb $0, %al: Clears the %al register, which is used for vararg functions like printf.
  • callq _printf: Calls the printf function.
  • xorl %eax, %eax: Sets the return value to 0.
  • popq %rbp: Restores the base pointer, a standard function epilogue.
  • retq: Returns from the function.
  • L_.str: .asciz "Hello, World!": Defines the string literal used in printf.

2. The For Loop – A Closer Look

C Code:

Assembly Code:

Assembly Breakdown:

  • subq $16, %rsp: Allocates 16 bytes of space on the stack for local variables.
  • movl $0, -4(%rbp): Initializes a local variable, likely for the return value.
  • movl $0, -8(%rbp): Initializes the loop counter i to 0.
  • LBB0_1:: Label marking the beginning of the loop.
  • cmpl $10, -8(%rbp): Compares the loop counter with 10.
  • jge LBB0_4: Jumps to the end of the loop if the counter is greater than or equal to 10.
  • Inside the loop, %esi and %rdi are set up for the printf call, which prints the counter.
  • The counter is incremented (addl $1, %eax) and the loop continues (jmp LBB0_1).
  • LBB0_4:: Label marking the end of the loop.

3. If-Else: The Decision-Making Drama

C Code:

Assembly Code:

Assembly Breakdown:

  • movl $10, -8(%rbp) and movl $20, -12(%rbp): Initialize variables a and b.
  • cmpl -12(%rbp), %eax: Compares a and b.
  • jle LBB0_2: Jumps to the else block if a is not greater than b.
  • If a > b, it executes the printf for "a is greater", otherwise, it prints "b is greater or equal".
  • The function concludes with standard stack cleanup and return.


The How-To: Setting Up and Diving In

Eager to try it yourself? Here's how I approached it:

  1. Crafting the C Code : Using VS Code, I wrote the C programs.
  2. The Magic of GCC Compilation : A simple command in the terminal that gives you an assembly version of your C code.

gcc -S -o output.s yourprogram.c         

In Conclusion: A Journey of Insight and Intrigue

What started as a curious peek into Piyush Itankar's interview experience turned into a deep dive into the nuts and bolts of programming. It's fascinating how a simple interview anecdote can lead to such an enriching learning experience. Who knew converting C to assembly could be this entertaining and enlightening?


#embeddedsystem #cprogramming #c2assembly #assemblylanguage #firmwareengineering

Shariful Islam

Graduate Research Assistant | Embedded Firmware Developer

1 年

Can you share any link to the vlog you mentioned? Thanks

回复
Yashwanth Naidu Tikkisetty

Embedded Software Engineer | MSc Embedded Systems

1 年

Interesting read, I have done a similar thing a while back to dive into the assembly and explore how the code was converted: https://cthecosmos.com/2022/10/29/peeking-into-the-assembly/

回复
Piyush Itankar

Embedded Systems @Google | pyjamabrah.com

1 年

Kiran Jojare ?? this is so much fun! I see you generated the assembly for x86. try it for ARM or RISC-V the assembly is more readable and relatable… ??

要查看或添加评论,请登录

Kiran Jojare的更多文章

社区洞察

其他会员也浏览了