! File: avg.m ! Dir: cis235/Examples ! Date: Sept 11, 2002 ! Author: DSS ! Computer: KUNET suns ! Assembler: as under the gcc compiler ! Compile: sa avg ! Execute: avg ! Purpose: input 2 ints, output them, and their average ! Updates: ! Used macros ! filled some delay slots ! saved the realoading of the variables define(value1Reg,%l1) define(value2Reg,%l2) .data .align 4 prompt: .asciz "Enter 2 integers\n" outformat: .asciz "Value 1=%d, Value 2=%d, Average: %5d \n" .align 4 informat: .asciz "%d %d" .align 4 value1: .word 0 value2: .word 0 .global main main: save %sp,-96,%sp set prompt,%o0 call printf nop !scanf(informat, &value1,&value2); set informat, %o0 set value1, %o1 set value2, %o2 !values are in addresses of value1 and value2 call scanf nop ! can't fill this one (with a set) !compute avg set value1,value1Reg ld [value1Reg],value1Reg set value2,value2Reg ld [value2Reg],value2Reg add value1Reg,value2Reg,%o0 !get dividend into o0 call .div !call division, but first... ! nop !delay slot filled mov 2,%o1 !get divisor set; fill delay slot mov %o0,%o3 !put quotient into %o3 for output !printf(outformat,value1,value2,avg) ! Put format string in o0 register, variables in %o_ ! then call printf function set outformat,%o0 !put format string in %o0 mov value1Reg,%o1 !let's not load again call printf,0 !print it mov value2Reg,%o2 ! save the load, fill a delay slot! ! nop !delay slot filled done: call exit, 0 !exit routine mov 0, %o0 !move return code into position during delay