/******************************************************* * File: par_reg.m * Dir: cis235/suns/ch07 * Date: January 4, 1999 * Author: HGG * Computer: KUNET suns * Assembler: sa par_reg * Compile: gcc * Execute: a.out * Purpose: To demonstrate parameter passing using * the system registers * * main reads two numbers x and y, and calls * the function f(x,y,&result) to compute * x*y + 2 and return the value in result. *******************************************************/ .data .align 8 prompt: .asciz "=>" .align 8 formati: .asciz "%d %d" formato: .asciz "f(X = %d,Y = %d, Result = %d )\n\n" /****** constants and offsets **********************/ define(x, -20) ! main local variables define(y, -24) ! problems if we start at -4 define(result, -28) ! for registers .align 8 .text .global main main: save %sp, -120 & -8, %sp loop: !while(printf(prompt), scanf(formati, &x, &y) != 2 !printf(prompt); sethi %hi(prompt), %o0 call printf,0 or %o0, %lo(prompt), %o0 ! scanf(formati, &x, &y) != 2 sethi %hi(formati),%o0 or %o0,%lo(formati),%o0 ! o0 = &formati add %fp,x,%o1 ! o1 = &x call scanf,0 add %fp,y,%o2 ! o2 = &y cmp %o0, 2 ! o0 == 2? bne done nop ! f(x, y, &result) result is a reference parameter ld [%fp + x], %o0 ! param1 = x ld [%fp + y], %o1 ! param2 = y call f, 0 add %fp, result, %o2 ! param3 = &result ! printf(formato, x, y, result); sethi %hi(formato),%o1 or %o1,%lo(formato),%o0 ld [%fp+x], %o1 ld [%fp+y], %o2 call printf,0 ld [%fp+result], %o3 b loop nop !********************************************************* done: ! exit call exit,0 mov 0, %o0 !********************************************************* !* The function f(param1, param2, &result) !* This function simply computes x*y + 2; !* since .mul is used, f saves and restores !* register g1 .align 4 .global f f: save %sp,-112,%sp mov %g1, %l0 ! save g1 mov %i0, %o0 mov %i1, %o1 call .mul ! o1 = param2 mov 2,%o3 add %o0,%o3,%o0 ! o0 = param1 * param2 + 2 st %o0,[%i2] ! address of result has answer mov %l0, %g1 ! restore g1 ret restore