TITLE "demo of binary to decimal" ; lightly optimized version ; This code makes use of values known to be in registers, ; eliminating many local variables. ; Overcommenting has been largely eliminated; ; this style and level of optimization offers a ; compromise between readability and performance. USE "hawk.h" USE "stdio.h" USE "stdlib.h" INT MAIN S MAIN ; activation record structure holding local variables of MYDEC ;RETAD = 0 DIGIT = 4 ; int digit, least sig digit of i ARSIZE = 8 MYDEC: ; expects: R3 = i, number to print STORES R1,R2 ; -- save RETAD ; -- param i already in R3 LIS R4,10 ; -- param 10 ADDI R2,R2,ARSIZE LIL R1,DIVIDEU JSRS R1,R1 ; top = divideu( i, 10 ) gets quot, rem ADDI R2,R2,-ARSIZE STORE R4,R2,DIGIT ; digit = i % 10 -- second return from divideu TESTR R3 ; -- check top BZS ENDIF ; if (top != 0) { ; -- parameter top already in R3 ADDI R2,R2,ARSIZE JSR R1,MYDEC ; mydec( top ) -- recursive call ADDI R2,R2,-ARSIZE ENDIF: ; } LOAD R3,R2,DIGIT ADDI R3,R3,'0' ; -- parameter digit+'0' ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ; putchar( '0' + digit ) -- regular call ADDI R2,R2,-ARSIZE LOADS PC,R2 ; return ; activation record structure holding local variables of MAIN ;RETAD = 0 ARSIZE = 4 MAIN: ; no parameters STORES R1,R2 LIW R3,123456789 ADDSI R2,ARSIZE JSR R1,MYDEC ; mydec( 123456789 ) ADDSI R2,-ARSIZE LOADS PC,R2 ; return END