TITLE "demo of binary to decimal" ; agressively optimized version ; the optimizations used here are probably confusing ; new assembly language programmers should avoid going this far! 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 ; least sig digit of i ARSIZE = 8 MYDEC: ; expects: R3 = i, integer to print STORES R1,R2 ADDI R2,R2,ARSIZE ; -- param i already in R3 LIS R4,10 ; -- param 10 LIL R1,DIVIDEU JSRS R1,R1 ; top = divideu( i, 10 ) gets quot, rem STORE R4,R2,DIGIT-ARSIZE ; digit = i % 10 -- also return value TESTR R3 ; -- check top BZS ENDIF ; if (top != 0) { ; -- parameter top already in R3 JSR R1,MYDEC ; mydec( top ) -- recursive call ENDIF: ; } LOAD R3,R2,DIGIT-ARSIZE ADDI R3,R3,'0' ; -- parameter digit+'0' LIL R1,PUTCHAR JSRS R1,R1 ; putchar( '0' + digit ) 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