Wednesday 26 November 2014

Converting a byte value to ascii in 6809 : Part 2

In the previous post, the code I wrote worked fine but was not optimal. Christopher Tumber pointed out the "trf a,b" instruction was redundant at the beginning of the code. Also there was some duplication of code when storing some of the digits, this would make the code larger than it needs to be. So I've been back over the code and fixed it.

Edit:
Also Gauze mentioned a mistake, where I used $80 (which is a memory location rather than a value) as the terminator for the string - it should be #$80 !



; Convert 8bit value to ASCII ;Requires 5 bytes of ram to save each digit to ;Debug_0 (sign) ;Debug_1 (Digit 1) ;Debug_2 (Digit 2) ;Debug_3 (Digit 3) ;Debug_4 (string terminator) ;Convert Register A to a 3 byte Decimal for printing to screen Reg_To_Decimal: tsta ;test a to check if neg - we would of needed to do this if we were sure A has just been loaded bpl Reg_DA ;branch if plus (positive) nega ;else is negative so negate a to be positive ldb #45 ;"-" sign bra Reg_D0 Reg_DA: ldb #43 ;"+" sign Reg_D0: stb Debug_0 ;store the sign tfr a,b suba #100 bge Reg_D1 ;if 0 or Greater first digit = 1 lda #48 ;else it = 0 = (ascii 48) bra Reg_D2 Reg_D1: tfr a,b ;copy remaining number after subtraction of 100 to b lda #49 ;set digit to 1+ascii (ascii 49) Reg_D2: sta Debug_1 ;store the first digit clra ;clear a is the counter Reg_D2_Loop: ;Div10 inca ;this might mean our count is 1 more than it should be subb #10 ;take away 10 until it is less than or equal to 0 bge Reg_D2_Loop ;if b is 0 or greater adda #47 ;Result is 1 higher than required so add ascii 48-1 sta Debug_2 ;a count is the middle digit -store it ;b must now contain the final digit but it will be negative, we need to add 10 to b + ascii addb #58 ;add 10+ascii stb Debug_3 ;store the last digit lda #$80 ;string terminator (this will tell a print routine this is the end of the string) sta Debug_4 rts

No comments:

Post a Comment