Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Assembly Language v C and Embedded Development: !gcc for PIC16F84



On 10 March 2011 11:13, Lewske Wada <ryu@example.com> wrote:

> How about
>
> mov bx, ax
> shl bx, 3
> sub bx, ax
>
> to get 7 times ax?

: josh@example.com; cat tmp/arithmetic.c
int multiply(const int multiplier) {
  int multiplicand = 12;
  return multiplicand * multiplicand;
}

int main(void) {
  int a = 5;
  return multiply(a);
}
: josh@example.com; gcc -O9 -S -o tmp/arithmetic.s tmp/arithmetic.c
: josh@example.com; cat tmp/arithmetic.s
	.file	"arithmetic.c"
	.text
	.p2align 4,,15
.globl multiply
	.type	multiply, @function
multiply:
.LFB0:
	.cfi_startproc
	movl	$144, %eax
	ret
	.cfi_endproc
.LFE0:
	.size	multiply, .-multiply
	.p2align 4,,15
.globl main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	movl	$144, %eax
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5"
	.section	.note.GNU-stack,"",@progbits


Strip out the unnecessary strings and you have a pretty efficient
program. Notice that the optimiser didn't even need a register to hold
the multiplicand and multiplier, and inlined the function call to
boot. gcc's version has to deal with x86 calling conventions, saving
main's return value in the %eax register; take that out, and it is
comparable to yours. Better in the context of a full program, I'd say,
since it knows which variables it can optimise away and not even waste
a register on.

Cheers,
Josh


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links