;----------------------------------------------------------------- ; EX78.ASM Binary to BCD Conversion. ; Parameter Passing via the Stack. ; ; C.P. Diduch 2001 ; ; This program calls procedure BIN2BCD(unsigned DATA, unsigned ; *PTR), to convert the 8-bit unsigned binary number passed as ; the 8-LSB's of DATA into its BCD representation. The starting ; address of the three BCD digits written by BIN2BCD() is passed ; as PTR. For example if DATA = 11000111B then the BCD result is ; written as the words 0x0001, 0x0009, 0x0009 beginning at ; address, PTR. ; ; DATA is loaded from input port 0x0000 and the BCD result is ; written to output port 0x0003. ; ; Author(s): ; Signature(s): Date: ;----------------------------------------------------------------- ; STACK and VARIABLES ; ; ; .ORG 0x0000 ; "varaiable assignments in RAM" .DS 5 ; "reserve RAM for the stack" TOS: .DS 1 ; DATA: .DS 1 ; unsigned DATA ; DIGITS: .DS 3 ; unsigned DIGITS[3] ; ; ;----------------------------------------------------------------- ; MAIN PROGRAM ; void main(void) { ; .ORG 0x8000 ; "assign the following to ROM" ; ADDL SP, NULL, TOS ; "initialize stack pointer" ; do { M0: LOADP R0, 0x0000 ; DATA = inport(0x0000); STOREM DATA, R0 ; SUBL SP, SP, 1 ; "push parameter DATA" DSTOREM (SP), R0 ; ADDL R0, NULL, DIGITS; SUBL SP, SP, 1 ; "push parameter &DIGITS[0]" DSTOREM (SP), R0 ; CALL BIN2BCD, RA ; BIN2BCD(DATA, &DIGITS) ADDL SP, SP, 2 ; "clean up stack" ADDL R9, NULL, DIGITS; DLOADM R0, (R9) ; // Pack BCD digits into R0. DLOADM R1, 1(R9) ; R0 = (DIGITS[2] << 8) + ADD R1, R1, R1 ; (DIGITS[1] << 4) + ADD R1, R1, R1 ; (DIGITS[0] ); ADD R1, R1, R1 ; ADD R1, R1, R1 ; DLOADM R2, 2(R9) ; ADD R2, R2, R2 ; ADD R2, R2, R2 ; ADD R2, R2, R2 ; ADD R2, R2, R2 ; ADD R2, R2, R2 ; ADD R2, R2, R2 ; ADD R2, R2, R2 ; ADD R2, R2, R2 ; OR R0, R0, R1 ; OR R0, R0, R2 ; STOREP 0x0002, R0 ; outport(0x0002, R0) JMP M0 ; } ; } M1: JMP M1 ; "program termination" ; ;----------------------------------------------------------------- ; PROCEDURE void BIN2BCD(unsigned DATA, unsigned *DIGITS) ; ; Procedure BIN2BCD(unsigned DATA, unsigned *PTR) converts the ; 8-bit unsigned binary number passed as the 8-LSB's of DATA on ; the stack into its 3-digit BCD representation. The 4-bit BCD ; digits are returned in the 4-LSB's of consecutive words in ; memory. The starting address of the 3-words is also passed on ; the stack. ; ; Typical CALL/LINKAGE sequence ; ; SUBL SP, SP, 1 ; "push DATA" ; DSTOREM (SP), R0 ; ; SUBL SP, SP, 1 ; "push &DIGITS[0]" ; DSTOREM (SP), R1 ; ; CALL BIN2BCD ; Y = BIN2BCD(DATA, &DIGITS[0]) ; ; ADDL SP, SP, 2 ; "clean up stack" ; ; Calls : no procedures. ; Called by : main(). ; Registers affected : R0, R1, R3, R4 and FL. ; Stack depth : 3 words. ;----------------------------------------------------------------- BIN2BCD: ; void BIN2BCD(unsigned DATA, unsigned *PTR) { ; SUBL SP, SP, 1 ; "save status" : push RA DSTOREM (SP), RA ; ; DLOADM R0, 1(SP) ; R0 = PTR ; DLOADM R1, 2(SP) ; R1 = DATA ; XOR# R3, R3, R3 ; HNDS = 0 ; XOR# R4, R4, R4 ; TENS = 0 ; ; B0: SUBL NULL, R1, 100 ; while (DATA > 100) { JMPC B1 ; SUBL R1, R1, 100 ; DATA = DATA - 100 ; ADDL R3, R3, 1 ; HNDS = HNDS + 1 ; JMP B0 ; } ; B1: SUBL NULL, R1, 10 ; while (DATA > 10) { JMPC B2 ; SUBL R1, R1, 10 ; DATA = DATA - 10 ; ADDL R4, R4, 1 ; TENS = TENS + 1 ; JMP B1 ; } ; B2: DSTOREM (R0), R1 ; DIGITS[0] = ONES; DSTOREM 1(R0), R4 ; DIGITS[1] = TENS; DSTOREM 2(R0), R3 ; DIGITS[2] = HNDS; ; DLOADM RA, (SP) ; "restore status" : pop RA ADDL SP, SP, 1 ; DJMP (RA) ; } ;-----------------------------------------------------------------