Convert program written in C into assembly

Posted

in

by

Sometimes to understand code , you need to look into assembly language. I write mostly using C. So sometimes to better understand the program i converte the program into assembly. The syntax changes from machine to machine.

Here is the program that i will use to convert to assembly using two different machine. One is ARM based and the other is x86_64.

#include <stdio.h>

struct CarDetail{
int CarNumber;
char CarOptions;
};

int main() {
struct CarDetail maruti_alto;

maruti_alto.CarNumber = 123;
maruti_alto.CarOptions = 'L';

printf("Maruti Alto Details\nNumber = \t%d\nOptions = \t%c",maruti_alto.CarNumber,maruti_alto.CarOptions);
return 0;
}

ARM

If i convert the above program using ARM based machine using command

gcc main.c -S -o main.s
        .arch armv6
	.file	"main.c"
	.text
	.section	.rodata
	.align	2
.LC0:
	.ascii	"Maruti Alto Details\012Number = \011%d\012Options ="
	.ascii	" \011%c\000"
	.text
	.align	2
	.global	main
	.arch armv6
	.syntax unified
	.arm
	.fpu vfp
	.type	main, %function
main:
	@ args = 0, pretend = 0, frame = 8
	@ frame_needed = 1, uses_anonymous_args = 0
	push	{fp, lr}
	add	fp, sp, #4
	sub	sp, sp, #8
	mov	r3, #123
	str	r3, [fp, #-12]
	mov	r3, #76
	strb	r3, [fp, #-8]
	ldr	r3, [fp, #-12]
	ldrb	r2, [fp, #-8]	@ zero_extendqisi2
	mov	r1, r3
	ldr	r0, .L3
	bl	printf
	mov	r3, #0
	mov	r0, r3
	sub	sp, fp, #4
	@ sp needed
	pop	{fp, pc}
.L4:
	.align	2
.L3:
	.word	.LC0
	.size	main, .-main
	.ident	"GCC: (Raspbian 8.3.0-6+rpi1) 8.3.0"
	.section	.note.GNU-stack,"",%progbits

If we want to remove relative addressing modes and make it very simple you can use -fomit-frame-pointer option

gcc main.c -S -fomit-frame-pointer -o main-omit-fp.s
        .arch armv6
	.file	"main.c"
	.text
	.section	.rodata
	.align	2
.LC0:
	.ascii	"Maruti Alto Details\012Number = \011%d\012Options ="
	.ascii	" \011%c\000"
	.text
	.align	2
	.global	main
	.arch armv6
	.syntax unified
	.arm
	.fpu vfp
	.type	main, %function
main:
	@ args = 0, pretend = 0, frame = 8
	@ frame_needed = 0, uses_anonymous_args = 0
	str	lr, [sp, #-4]!
	sub	sp, sp, #12
	mov	r3, #123
	str	r3, [sp]
	mov	r3, #76
	strb	r3, [sp, #4]
	ldr	r3, [sp]
	ldrb	r2, [sp, #4]	@ zero_extendqisi2
	mov	r1, r3
	ldr	r0, .L3
	bl	printf
	mov	r3, #0
	mov	r0, r3
	add	sp, sp, #12
	@ sp needed
	ldr	pc, [sp], #4
.L4:
	.align	2
.L3:
	.word	.LC0
	.size	main, .-main
	.ident	"GCC: (Raspbian 8.3.0-6+rpi1) 8.3.0"
	.section	.note.GNU-stack,"",%progbits

X86_64

gcc main.c -S -o main.s
	.file	"main.c"
	.text
	.section	.rodata
	.align 8
.LC0:
	.string	"Maruti Alto Details\nNumber = \t%d\nOptions = \t%c"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
	movl	$123, -8(%rbp)
	movb	$76, -4(%rbp)
	movzbl	-4(%rbp), %eax
	movsbl	%al, %edx
	movl	-8(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Debian 8.3.0-6) 8.3.0"
	.section	.note.GNU-stack,"",@progbits
gcc main.c -S -fomit-frame-pointer -o main-omit-fp.s
	.file	"main.c"
	.text
	.section	.rodata
	.align 8
.LC0:
	.string	"Maruti Alto Details\nNumber = \t%d\nOptions = \t%c"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$123, 8(%rsp)
	movb	$76, 12(%rsp)
	movzbl	12(%rsp), %eax
	movsbl	%al, %edx
	movl	8(%rsp), %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Debian 8.3.0-6) 8.3.0"
	.section	.note.GNU-stack,"",@progbits

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *