SHARE
TWEET

Multiple Hex to Decimal Conversions

slydogstudios May 13th, 2013 177 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Routine to change multiple hex numbers to decimal in order to display them on the screen
  2. ; This is a modified version of the Nerdy Nights tutorial on the same subject. See here:
  3. ; http://nintendoage.com/forum/messageview.cfm?catid=22&threadid=33308
  4. ; This routine is zeropage heavy, but is being used for an RPG-style game, so it is a lot
  5. ;  of what the game is related to. If you can't use as much zeropage as this, then at the
  6. ;  very least the reserved bytes with the ";;" following them is a must to run this code
  7. ;  properly.
  8.  
  9. ; ZEROPAGE
  10. temp_8bit_1:            .res 1
  11. ones_prelim:            .res 1
  12. tens_prelim:            .res 1
  13.  
  14. hp_han:                 .res 1
  15. hp_hubs:                .res 1
  16. hp_hekl:                .res 1
  17. mp_han:                 .res 1
  18. mp_hubs:                .res 1
  19. mp_hekl:                .res 1
  20. attack_han:             .res 1
  21. attack_hubs:            .res 1
  22. attack_hekl:            .res 1
  23. defense_han:            .res 1
  24. defense_hubs:           .res 1
  25. defense_hekl:           .res 1
  26. speed_han:              .res 1
  27. speed_hubs:             .res 1
  28. speed_hekl:             .res 1
  29.  
  30. han_tens_hp:            .res 1  ;;
  31. hubs_tens_hp:           .res 1  ;;
  32. hekl_tens_hp:           .res 1  ;;
  33. han_ones_hp:            .res 1  ;;
  34. hubs_ones_hp:           .res 1  ;;
  35. hekl_ones_hp:           .res 1  ;;
  36. han_tens_mp:            .res 1  ;;
  37. hubs_tens_mp:           .res 1  ;;
  38. hekl_tens_mp:           .res 1  ;;
  39. han_ones_mp:            .res 1  ;;
  40. hubs_ones_mp:           .res 1  ;;
  41. hekl_ones_mp:           .res 1  ;;
  42. han_tens_att:           .res 1  ;;
  43. hubs_tens_att:          .res 1  ;;
  44. hekl_tens_att:          .res 1  ;;
  45. han_ones_att:           .res 1  ;;
  46. hubs_ones_att:          .res 1  ;;
  47. hekl_ones_att:          .res 1  ;;
  48. han_tens_def:           .res 1  ;;
  49. hubs_tens_def:          .res 1  ;;
  50. hekl_tens_def:          .res 1  ;;
  51. han_ones_def:           .res 1  ;;
  52. hubs_ones_def:          .res 1  ;;
  53. hekl_ones_def:          .res 1  ;;
  54. han_tens_spd:           .res 1  ;;
  55. hubs_tens_spd:          .res 1  ;;
  56. hekl_tens_spd:          .res 1  ;;
  57. han_ones_spd:           .res 1  ;;
  58. hubs_ones_spd:          .res 1  ;;
  59. hekl_ones_spd:          .res 1  ;;
  60.  
  61. ; Do this in the loop somewhere...
  62.         ldy #$00
  63.         sty tens_prelim
  64.         sty ones_prelim
  65.         jsr player_conv
  66.  
  67. ; ... and here is the subroutine
  68. ; INITIALIZE 'Y' REGISTER AND ONES_ AND TENS_
  69. ;  PRELIM TO 0 BEFORE CALLING THIS ROUTINE
  70. player_conv:
  71.         lda hp_han, y                   ; Load A with character stat
  72.         sta temp_8bit_1                 ; Store it in temp to preserve character stat in RAM
  73. @next_player_conv:                      ;  because the temp will be changed by end of routine
  74.         lda temp_8bit_1                 ; Must load temp again because of looping
  75.         cmp #$0a                        ; Compare temp to decimal 10
  76.         bcc @do_ones                    ; If it's less than 10, branch to the ones
  77.                 lda temp_8bit_1         ; If not, subtract by ten to get the number
  78.                 sec                     ;  widdled down to a single digit.
  79.                 sbc #$0a                ;
  80.                 sta temp_8bit_1         ;
  81.                 inc tens_prelim         ; Increment tens_prelim each iteration to create single digit for tens spot
  82.                 jmp @next_player_conv   ; Jump back to see if we need to subtract by 10 again
  83. @do_ones:
  84.         lda temp_8bit_1                 ; Load A with temp again and store it in ones_prelim
  85.         sta ones_prelim                 ;  since number is already a single digit
  86.  
  87.         ldx hex_tens_prelim, y          ; Load X with the table hex_tens_prelim offset by Y for using
  88.         lda tens_prelim                 ;  the zeropage tables
  89.         clc                             ; Clear the carry to ready for addition
  90.         adc #$30                        ; Add #$30 to the tens_prelim to adjust tile number for showing
  91.         sta $00, x                      ;  digit on-screen and store into correct zeropage RAM slot
  92.  
  93.         ldx hex_ones_prelim, y          ; Load X with the table hex_ones_prelim offset by Y for using
  94.         lda ones_prelim                 ;  the zeropage tables
  95.         clc                             ; Clear the carry to ready for addition
  96.         adc #$30                        ; Add #$30 to the ones_prelim to adjust tile number for showing
  97.         sta $00, x                      ;  digit on-screen and store into correct zeropage RAM slot
  98.  
  99.         lda #$00                        ; Clear ones_prelim and tens_prelim to prepare them for use
  100.         sta tens_prelim                 ;  on the next character stat to be used in the loop
  101.         sta ones_prelim                 ;
  102.  
  103.         iny                             ; Increment Y and check if has reached decimal 15 yet
  104.         cpy #$0f                        ; If it hasn't, then branch back to player_conv and run
  105.         bne player_conv                 ;  the hex to decimal routine again
  106.         rts                             ; Return when loop is complete
  107.  
  108. ; Zeropage tables for use of storing the digits. These variables are the
  109. ;  ones necessary to be in ZP for the code to run properly
  110. hex_tens_prelim:
  111.         .byte <han_tens_hp,<hubs_tens_hp,<hekl_tens_hp
  112.         .byte <han_tens_mp,<hubs_tens_mp,<hekl_tens_mp
  113.         .byte <han_tens_att,<hubs_tens_att,<hekl_tens_att
  114.         .byte <han_tens_def,<hubs_tens_def,<hekl_tens_def
  115.         .byte <han_tens_spd,<hubs_tens_spd,<hekl_tens_spd
  116. hex_ones_prelim:
  117.         .byte <han_ones_hp,<hubs_ones_hp,<hekl_ones_hp
  118.         .byte <han_ones_mp,<hubs_ones_mp,<hekl_ones_mp
  119.         .byte <han_ones_att,<hubs_ones_att,<hekl_ones_att
  120.         .byte <han_ones_def,<hubs_ones_def,<hekl_ones_def
  121.         .byte <han_ones_spd,<hubs_ones_spd,<hekl_ones_spd
RAW Paste Data
Top