Advertisement
Nightseeker

bin2dec.s

Mar 15th, 2021
4,574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. PORTB = $6000
  2. PORTA = $6001
  3. DDRB = $6002
  4. DDRA = $6003
  5.  
  6. value = $0200 ; 2 bytes
  7. mod10 =$0202 ; 2 bytes
  8. message = $0204 ; 6 bytes
  9.  
  10. E  = %10000000
  11. RW = %01000000
  12. RS = %00100000
  13.  
  14.   .org $8000
  15.  
  16. reset:
  17.   ldx #$ff
  18.   txs
  19.  
  20.   lda #%11111111 ; Set all pins on port B to output
  21.   sta DDRB
  22.   lda #%11100000 ; Set top 3 pins on port A to output
  23.   sta DDRA
  24.  
  25.   lda #%00111000 ; Set 8-bit mode; 2-line display; 5x8 font
  26.   jsr lcd_instruction
  27.   lda #%00001110 ; Display on; cursor on; blink off
  28.   jsr lcd_instruction
  29.   lda #%00000110 ; Increment and shift cursor; don't shift display
  30.  jsr lcd_instruction
  31.  lda #$00000001 ; Clear display
  32.  jsr lcd_instruction
  33.  
  34.  lda #0
  35.  sta message
  36.  
  37.  ; Initialize value to be the number to convert
  38.  lda number
  39.  sta value
  40.  lda number + 1
  41.  sta value + 1
  42.  
  43. divide:
  44.  ; Initialize the remainder to zero
  45.  lda #0
  46.  sta mod10
  47.  sta mod10 +1
  48.  clc
  49.  
  50.  ldx #16
  51. divloop:
  52.  ; Rotate quotient and remainder
  53.  rol value
  54.  rol value +1
  55.  rol mod10
  56.  rol mod10 +1
  57.  
  58.  ; a,y = dividend - divisor
  59.  sec
  60.  lda mod10
  61.  sbc #10
  62.  tay ; save low byte in Y
  63.  lda mod10 + 1
  64.  sbc #0
  65.  bcc ignore_result ; branch if dividend < divisor
  66.  sty mod10
  67.  sta mod10 + 1
  68.  
  69. ignore_result:
  70.  dex
  71.  bne divloop
  72.  rol value ; shift in the last bit of the quotient
  73.  rol value + 1
  74.  
  75.  lda mod10
  76.  clc
  77.  sbc #"0"
  78.  jsr push_char
  79.  
  80.  ; if value ! = 0, then continue dividing
  81.  lda value
  82.  ora value + 1
  83.  bne divide ; branch if value not zero
  84.  
  85.  ldx #0
  86. print:
  87.  lda message,x
  88.  beq loop
  89.  jsr print_char
  90.  inx
  91.  jmp print
  92.  
  93. loop:
  94.  jmp loop
  95.  
  96. number: .word 1729
  97.  
  98.  ; Add the character in the A register to the beginning of the
  99.  ; null terminated string 'message'
  100. push_char:
  101.  pha ; Push new first char onto stack
  102.  ldy #0
  103.  
  104. char_loop:
  105.  lda message,y ; Get char on string and put into X
  106.  tax
  107.  pla
  108.  sta message,y ; Pull char off stack and add it to the string
  109.  iny
  110.  tax
  111.  pha           ; Push char from string onto stack
  112.  bne char_loop
  113.  
  114.  pla
  115.  sta message,y ; Pull the null off the stack and add to the end of the string
  116.  
  117.  rts
  118.  
  119. lcd_wait:
  120.  pha
  121.  lda #%00000000  ; Port B is input
  122.  sta DDRB
  123. lcdbusy:
  124.  lda #RW
  125.  sta PORTA
  126.  lda #(RW | E)
  127.  sta PORTA
  128.  lda PORTB
  129.  and #%10000000
  130.  bne lcdbusy
  131.  
  132.  lda #RW
  133.  sta PORTA
  134.  lda #%11111111  ; Port B is output
  135.  sta DDRB
  136.  pla
  137.  rts
  138.  
  139. lcd_instruction:
  140.  jsr lcd_wait
  141.  sta PORTB
  142.  lda #0         ; Clear RS/RW/E bits
  143.  sta PORTA
  144.  lda #E         ; Set E bit to send instruction
  145.  sta PORTA
  146.  lda #0         ; Clear RS/RW/E bits
  147.  sta PORTA
  148.  rts
  149.  
  150. print_char:
  151.  jsr lcd_wait
  152.  sta PORTB
  153.  lda #RS         ; Set RS; Clear RW/E bits
  154.  sta PORTA
  155.  lda #(RS | E)   ; Set E bit to send instruction
  156.  sta PORTA
  157.  lda #RS         ; Clear E bits
  158.  sta PORTA
  159.  rts
  160.  
  161.  .org $fffc
  162.  .word reset
  163.  .word $0000
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement