/* Original C source "factorial.c": #include void main() { int n, accumulator; printf("Input number: "); scanf("%4d",&n); accumulator = 1; while (n > 1) { accumulator = accumulator * n; n = n - 1; } printf("\nThe factorial value is %d\n",accumulator); } */ /* This file was created with the command "gcc -S factorial.c". It can be assembled into an executable with "gcc myfact.s -o myfact". "myfact" will be the name of the executable. */ .file "factorial.c" gcc2_compiled.: .section ".rodata" .align 4 ! changed from 8 .LLC0: .asciz "Input number: " .align 4 .LLC1: .asciz "%4d" .global .umul .align 4 .LLC2: .asciz "\nThe factorial value is %d\n" .section ".text" ! start of source code .align 4 ! all instructions are 4 bytes long .global main .type main,#function .proc 020 main: save %sp,-120,%sp ! create stack frame sethi %hi(.LLC0),%o1 ! load high 22 bits into o1 or %o1,%lo(.LLC0),%o0 ! effect: load low 10 bits into o0 call printf,0 ! call printf library routine nop ! pipeline delay slot add %fp,-20,%o1 ! fp-20 = address of "int n" sethi %hi(.LLC1),%o2 or %o2,%lo(.LLC1),%o0 call scanf,0 ! call scanf with args in o0,o1 nop mov 1,%o0 ! prepare 1 for storage st %o0,[%fp-24] ! fp-24 = "int accumulator" .LL2: ! while loop test (n > 1) ld [%fp-20],%o0 ! / compare n with 1 cmp %o0,1 ! \ bg .LL4 ! branch if > to .LL4 nop ! pipeline delay slot b .LL3 ! branch if not > to .LL3 nop .LL4: ! body of while loop ld [%fp-24],%o0 ! / n*accumulator ld [%fp-20],%o1 call .umul,0 nop st %o0,[%fp-24] ! \ result of * in o0 ld [%fp-20],%o0 ! / n = n -1 add %o0,-1,%o1 st %o1,[%fp-20] ! \ b .LL2 ! jump back to while loop test nop .LL3: ! end of while loop sethi %hi(.LLC2),%o1 ! / prints accumulator or %o1,%lo(.LLC2),%o0 ld [%fp-24],%o1 call printf,0 ! \ nop .LL1: ret ! end of function code (return) restore .LLfe1: .size main,.LLfe1-main .ident "GCC: (GNU) 2.8.0"