User:Qalle/Algorithms: Difference between revisions

From NESdev Wiki
Jump to navigationJump to search
(create)
 
(add algorithm)
Line 1: Line 1:
Miscellaneous algorithms that may be useful on the NES.
Miscellaneous algorithms that may be useful on the NES.
== 8-bit unsigned integer to 2 hexadecimal ASCII digits ==
byte2hex:
    ; in: A = unsigned integer
    ; out: X/A = ASCII digits of sixteens/ones in upper case
    pha
    lsr a
    lsr a
    lsr a
    lsr a
    clc
    jsr nybble2hex
    tax
    pla
    and #%00001111
    jsr nybble2hex
    rts
nybble2hex:
    adc #$30        ; ASCII("0")
    cmp #$3a        ; ASCII("9")+1
    bcc +
    adc #7          ; ASCII("A")-(ASCII("9")+1)
+  rts


== 8-bit unsigned integer to 3 decimal ASCII digits ==
== 8-bit unsigned integer to 3 decimal ASCII digits ==
Source: <ref>[https://codebase64.org/doku.php?id=base:tiny_.a_to_ascii_routine Tiny .A to ASCII routine], Codebase64</ref>
Source: <ref>[https://codebase64.org/doku.php?id=base:tiny_.a_to_ascii_routine Tiny .A to ASCII routine], Codebase64</ref>
  ; in: A = unsigned integer
byte2dec:
  ; out: Y/X/A = ASCII digits of hundreds/tens/ones
    ; in: A = unsigned integer
  ldy #$2f  ; ASCII("0") - 1
    ; out: Y/X/A = ASCII digits of hundreds/tens/ones
  ldx #$3a  ; ASCII("9") + 1
    ldy #$2f  ; ASCII("0")-1
  sec
    ldx #$3a  ; ASCII("9")+1
  - iny
    sec
  sbc #100
  -   iny
  bcs -
    sbc #100
  - dex
    bcs -
  adc #10
  -   dex
  bmi -
    adc #10
  adc #$2f  ; ASCII("0")
    bmi -
    adc #$2f  ; ASCII("0")
    rts


== References ==
== References ==
<references/>
<references/>

Revision as of 19:49, 21 October 2023

Miscellaneous algorithms that may be useful on the NES.

8-bit unsigned integer to 2 hexadecimal ASCII digits

byte2hex:
    ; in: A = unsigned integer
    ; out: X/A = ASCII digits of sixteens/ones in upper case
    pha
    lsr a
    lsr a
    lsr a
    lsr a
    clc
    jsr nybble2hex
    tax
    pla
    and #%00001111
    jsr nybble2hex
    rts
nybble2hex:
    adc #$30        ; ASCII("0")
    cmp #$3a        ; ASCII("9")+1
    bcc +
    adc #7          ; ASCII("A")-(ASCII("9")+1)
+   rts

8-bit unsigned integer to 3 decimal ASCII digits

Source: [1]

byte2dec:
    ; in: A = unsigned integer
    ; out: Y/X/A = ASCII digits of hundreds/tens/ones
    ldy #$2f  ; ASCII("0")-1
    ldx #$3a  ; ASCII("9")+1
    sec
-   iny
    sbc #100
    bcs -
-   dex
    adc #10
    bmi -
    adc #$2f  ; ASCII("0")
    rts

References