TITLE "demo of binary to decimal" ; Note: This code is seriously overcommented ; because it is intended for those only just learning ; about calling, receiving and return sequences. ; This code is agressively unoptimized; ; aside from using R2 to point to the activation record, ; register use is deliberately forgotten beween code blocks. USE "hawk.h" USE "stdio.h" USE "stdlib.h" INT MAIN S MAIN ; activation record structure holding local variables of MYDEC ; void mydec( unsigned int i ) { RETAD = 0 I = 4 ; int i, the parameter DIGIT = 8 ; int digit, least sig digit of i TOP = 12 ; int top, more sig digits of i ARSIZE = 16 MYDEC: ; -- start receiving sequence for mydec STORES R1,R2 ; -- save RETAD STORE R3,R2,I ; -- save and initialize I ; -- end receiving sequence for mydec ; -- start calling sequence for divideu LOAD R3,R2,I ; -- param I LIS R4,10 ; -- param 10 ADDI R2,R2,ARSIZE ; -- push AR for mydec LIL R1,DIVIDEU JSRS R1,R1 ; -- divideu( I, 10 ) gets quotient, remainder ADDI R2,R2,-ARSIZE ; -- pop AR for mydec STORE R3,R2,TOP ; top = I / 10 STORE R4,R2,DIGIT ; digit = I % 10 ; -- end calling sequence for divideu LOAD R3,R2,TOP CMPI R3,0 BZS ENDIF ; if (top != 0) { ; -- begin calling sequence for mydec LOAD R3,R2,TOP ; -- parameter ADDI R2,R2,ARSIZE ; -- push my AR LIL R1,MYDEC JSRS R1,R1 ; mydec( top ) -- recursive call ADDI R2,R2,-ARSIZE ; -- pop my AR ; -- end calling sequence for mydec ENDIF: ; } ; -- begin calling sequence for putchar LOAD R3,R2,DIGIT ADDI R3,R3,'0' ; -- parameter digit+'0' ADDI R2,R2,ARSIZE ; -- push my AR LIL R1,PUTCHAR JSRS R1,R1 ; putchar( '0' + digit ) -- regular call ADDI R2,R2,-ARSIZE ; -- pop my AR ; -- end calling sequence for putchar ; -- return sequence for mydec LOADS PC,R2 ; return ; -- end return sequence for mydec ; activation record structure holding local variables of MAIN ; void main() { RETAD = 0 ARSIZE = 4 MAIN: STORES R1,R2 ; -- receiving sequence ; -- calling sequence begins here LIW R3,123456789 ; -- pass a param ADDSI R2,ARSIZE ; -- push AR for main JSR R1,MYDEC ; mydec( 123456789 ) ADDSI R2,-ARSIZE ; -- pop AR for main ; -- calling sequence ends here LOADS PC,R2 ; --return sequence END