CSC352 GCC-Generated Assembly ARM & Pentium

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 08:22, 9 March 2017 (EST)



This page simply shows two example of assembly code, one generated by GCC for an ARM processor (Raspberry Pi 3), one generated by GCC for a Pentium-class processor.


C Program


#include <stdio.h>

int main( int argc, char* argv[] ) {
  int a, b, c;
  a = 3;
  b = 5;
  c = a + b;
  printf( "c=%d\n", c );
  return 0;
}


GCC on Raspberry PI 3


The command to generate the assembly listing file:

gcc -S hello.c
ls -l
-rw-r--r-- 1 pi pi  974 Mar  9 08:13 hello.s
-rwxr-xr-x 1 pi pi 5776 Mar  9 08:13 a.out
-rw-r--r-- 1 pi pi  142 Mar  9 08:12 hello.c

The assembly file (hello.s):

	.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	"c=%d\012\000"
	.text
	.align	2
	.global	main
	.type	main, %function
main:
	@ args = 0, pretend = 0, frame = 24
	@ frame_needed = 1, uses_anonymous_args = 0
	stmfd	sp!, {fp, lr}
	add	fp, sp, #4
	sub	sp, sp, #24
	str	r0, [fp, #-24]
	str	r1, [fp, #-28]
	mov	r3, #3
	str	r3, [fp, #-8]
	mov	r3, #5
	str	r3, [fp, #-12]
	ldr	r2, [fp, #-8]
	ldr	r3, [fp, #-12]
	add	r3, r2, r3
	str	r3, [fp, #-16]
	ldr	r0, .L3
	ldr	r1, [fp, #-16]
	bl	printf
	mov	r3, #0
	mov	r0, r3
	sub	sp, fp, #4
	@ sp needed
	ldmfd	sp!, {fp, pc}
.L4:
	.align	2
.L3:
	.word	.LC0
	.size	main, .-main
	.ident	"GCC: (Raspbian 4.9.2-10) 4.9.2"
	.section	.note.GNU-stack,"",%progbits


GCC on Pentium


The command is the same.

	.file	"hello0.c"
	.section	.rodata
.LC0:
	.string	"c=%d\n"
	.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	$32, %rsp
	movl	%edi, -20(%rbp)
	movq	%rsi, -32(%rbp)
	movl	$3, -12(%rbp)
	movl	$5, -8(%rbp)
	movl	-8(%rbp), %eax
	movl	-12(%rbp), %edx
	addl	%edx, %eax
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	movl	%eax, %esi
	movl	$.LC0, %edi
	movl	$0, %eax
	call	printf
	movl	$0, %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
	.section	.note.GNU-stack,"",@progbits


Note the differences between the two assembly files. Same C code, different assembly, and therefore different executable for both processor types.