;----------------------------------------------------------------- ; EX75.ASM Convert from binary to hex representation in ASCII. ; Parameter Passing via Register. ; ; C.P. Diduch 2001 ; ; This program calls procedure unsigned BIN2HEX(unsigned BIN) to ; convert the 4-LSB's of BIN to hex representation in ASCII. BIN ; is passed to BIN2HEX() in register R0, and the 7-bit ASCII value ; is returned in the 7-LSB's of register R1. BIN is loaded from ; input port 0x0000 and the hex value in ASCII is written to ; output port 0x0002. ; ;----------------------------------------------------------------- ; STACK and VARIABLES ; .ORG 0x0000 ; "variable assignments in RAM" .DS 5 ; "reserve RAM for the stack" TOS: .DS 1 ; X: .DS 1 ; unsigned X, Y ; Y: .DS 1 ; ;----------------------------------------------------------------- ; MAIN PROGRAM ; void main(void) { ; ; .ORG 0x8000 ; "assign the following to ROM" ; ADDL SP, NULL, TOS ; "initialize stack pointer" ; do { M0: LOADP R0, 0x0000 ; X = 0x000F & inport(0x0000) ; ANDL R0, R0, 0x000F ; STOREM X, R0 ; ; LOADM R0, X ; Y = BIN2HEX(X); CALL BIN2HEX, RA ; STOREM Y, R1 ; ; STOREP 0x0002, R1 ; outport(0x0002, Y); JMP M0 ; } ; } ;----------------------------------------------------------------- ; PROCEDURE unsigned BIN2HEX(unsigned BIN) ; ; BIN2HEX() converts the 4-LSB's of the value passed as BIN in R0 ; into its HEX representation in ASCII. The ASCII value is ; returned in register R1. ; ; Conversion Examples: BIN HEX ASCII(in hex) ; 0011 3 33 ; 0111 7 37 ; 1001 3 39 ; 1010 A 41 ; 1011 B 42 ; ; Typical CALL/LINKAGE sequence: ; ; LOADM R0, X ; ; CALL BIN2HEX, RA ; ; STOREM Y, R1 ; outport(BIN2HEX(X)) ; ; ; Calls : no procedures. ; Called by : main(). ; Registers affected : R1. ; Stack depth : 1 word. ;----------------------------------------------------------------- BIN2HEX: ; unsigned BIN2HEX(unsigned BIN) { ; SUBL# SP, SP, 1 ; "save status" : push FL DSTOREM (SP), FL ; ANDL R1, R0, 0x000F ; R1 = BIN & 0x000F ; ; B0: SUBL NULL, R1, 10 ; if (R1 < 10) { JMPNC B1 ; ; ADDL R1, R1, 0x0030 ; R1 = R1 + 0x0030 ; JMP B2 ; } ; else { B1: ADDL R1, R1, 0x0037 ; R1 = R1 + 0x0037 ; ; } B2: DLOADM FL, (SP) ; "restore status" : pop FL ADDL# SP, SP, 1 ; DJMP (RA) ; return R1; ; }