satpro

Print 16x16 Text

Feb 16th, 2026
3,578
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 9.29 KB | Source Code | 0 0
  1.  
  2.  
  3.  
  4.  
  5. ; ========================================================================================
  6.  
  7. ; Print16 params                Print16 (a)
  8. ; p1: a             ascii     r8        ascii character, 0-255
  9.  
  10. align 16
  11. export Print16:            ; print a 16x16 text character
  12.  
  13.   sub rsp, 28h
  14.   mov [rsp+00h], rsi
  15.   mov [rsp+08h], rdi
  16.   mov [rsp+10h], rbx
  17.   mov [rsp+18h], rbp
  18.  
  19. ; ----  Enter  -----
  20.  
  21.  
  22. ; 1. src
  23.  
  24. ; get a ptr to the font data
  25.   mov esi, FONT16$            ; ptr to font sets in emulator ram @ $A000
  26.  
  27. ; add offset to char bitmap data
  28.   mov ebx, r8d                ; preserve char# in r8
  29.   shl ebx, 5                  ; offset = char# * (32 bytes/char)
  30.   add esi, ebx                ; add offset = ptr to this character's bitmap data
  31.  
  32. ; 2. dst
  33.  
  34.   mov edi, VIDEO$             ; base
  35.  
  36. ; add (x,y) offsets
  37.   mov eax, [CursorX]          ; get (x)
  38.   mov ebx, [CursorY]          ; get (y)
  39.  
  40.   shl eax, 2                  ; x-offset = x * 4
  41.   shl ebx, 13                 ; y-offset = y * 2000h
  42.  
  43.   add edi, eax                ; add x-offset to base surface
  44.   add edi, ebx                ; add y-offset to base address
  45.  
  46.   mov ebp, edi                ; copy print address to rowptr
  47.  
  48.   mov r10d, [BGcolor]         ; get bg color
  49.   mov r11d, [FGcolor]         ; get fg color
  50.  
  51. ; 3.  -----  the print loop  -----
  52.  
  53.   mov dl, 16                  ; init rowctr (16 rows, 16 pixels each)
  54.  
  55. do16rows:           ; 16-bit processing of one screen row of char data
  56.   mov ax, [esi]               ; ax = bitmap data for 1 row (16 pixels, 2 bytes)
  57.   xchg al, ah                 ; swap the order of bytes for 16-bit access
  58.   mov cl, 16                  ; 16 pixels per screen row
  59.  
  60. do16pixels:         ; 16 shifts = 1 row (2 bytes) of pixel data, poke on 1-bits only
  61.   shl ax, 1                   ; shift bit 15 into carry flag
  62.   cmovnc ebx, r10d            ; when bit = 0, use bg color
  63.   cmovc ebx, r11d             ; when bit = 1, use fg color
  64.  
  65.   mov [edi], ebx              ; poke the color to surface memory
  66.  
  67.   add edi, 4                  ; advance to next x-pixel (right 1)
  68.   dec cl                      ; pixelctr = pixelctr-1
  69.   jnz < do16pixels            ; loop until pixelctr = 0
  70.  
  71. ; re-sync the pointers for the next row (down 1)
  72.   add esi, 2                  ; advance fontptr to next source row (+2 bytes)
  73.   add ebp, 2000h              ; rowptr + pitch = next row
  74.   mov edi, ebp                ; copy rowptr to colptr
  75.   dec dl                      ; rowctr = rowctr-1
  76.   jnz < do16rows              ; loop until rowctr = 0
  77.  
  78.   add D[CursorX], 16          ; get CursorX ready for next Print (+16 pixels)
  79.  
  80.  
  81. ; ----- EXIT -----
  82.  
  83.   mov rsi, [rsp+00h]
  84.   mov rdi, [rsp+08h]
  85.   mov rbx, [rsp+10h]
  86.   mov rbp, [rsp+18h]
  87.   add rsp, 28h
  88.   ret
  89.  
  90. ; ========================================================================================
  91.  
  92. ; Plot(x,y)
  93.  
  94. ; p1: x
  95. ; p2: y
  96.  
  97. align 16
  98. export Plot:
  99.  
  100.   mov [CursorX], r8d
  101.   mov [CursorY], r9d
  102.  
  103.   ret
  104.  
  105. ; ========================================================================================
  106.  
  107. ; SetTextColor(bg_color,fg_color)
  108.  
  109. ; p1: bg color
  110. ; p2: fg color
  111.  
  112. align 16
  113. export SetTextColor:
  114.  
  115.   mov [BGcolor], r8d
  116.   mov [FGcolor], r9d
  117.  
  118.   ret
  119.  
  120. ; ========================================================================================
  121.  
  122. ; SetFont(font_num)
  123.  
  124. ; p1: font# (0 = serif, 1 = sans)
  125.  
  126. align 16
  127. export SetFont:
  128.  
  129.   mov [FontNum], r8d
  130.  
  131.   ret
  132.  
  133. ; ========================================================================================
  134.  
  135. ; String16          String16 (pStringz)
  136.  
  137. ; p1: pStringz      r8        ptr to text string
  138.  
  139. align 16
  140. export String16:           ; String prints a 0-term ascii string
  141.  
  142.   sub rsp, 18h
  143.   mov [rsp+00h], rsi          ; preserve
  144.   mov [rsp+08h], rbx          ; preserve
  145.  
  146. ; -----  Enter  -----
  147.  
  148. ; loop to print a 0-term string
  149.   mov esi, r8d                ; get the string ptr
  150.   xor ebx, ebx                ; init string length index = 0
  151.  
  152. strloop:
  153.   movzx r8d, B[esi+ebx]       ; get a character from the string
  154.   or r8d, r8d                 ; check to see if character = 0
  155.   jz > strExit                ; if yes, exit
  156.  
  157.   call Print16                ; print the character in r8
  158.  
  159.   inc ebx                     ; increment string length index
  160.   jmp < strloop               ; always loop back for more
  161.  
  162.  
  163. ; -----  Exit  -----
  164.  
  165. strExit:
  166.   mov rsi, [rsp+00h]          ; restore
  167.   mov rbx, [rsp+08h]          ; restore
  168.   add rsp, 18h
  169.  
  170.   ret
  171.  
  172. ; ========================================================================================
  173.  
  174. ; GetStringLen (pStringz)
  175.   ; p1: pStringz    r8        ptr to 0-term text string
  176.   ; return:         eax       string's character count
  177.  
  178. align 16
  179. export GetStringLen:      ; GetStringLen (pStringz): returns the length of a 0-term string
  180.  
  181.   sub rsp, 18h
  182.   mov [rsp+00h], rsi          ; preserve
  183.   mov [rsp+08h], rbx          ; preserve
  184.  
  185. ; -----  Enter  -----
  186.  
  187.  
  188.   mov esi, r8d                ; get string ptr
  189.   xor eax, eax                ; init string index = 0
  190.  
  191. ; loop to gather a string
  192. gstrloop:
  193.   mov bl, [rsi+rax]           ; get a character from the string
  194.   or bl, bl                   ; is ascii char = 0?
  195.   jz > gstrExit               ; if = 0, exit loop with charcount in eax
  196.   inc eax                     ; if <> 0, increment charcount
  197.   jmp < gstrloop              ; always loop to get another char
  198.  
  199.  
  200. ; -----  Exit  -----
  201.  
  202. gstrExit:
  203.   mov rsi, [rsp+00h]          ; preserve
  204.   mov rbx, [rsp+08h]          ; preserve
  205.   add rsp, 18h
  206.   ret
  207.  
  208. ; ========================================================================================
  209.  
  210. ; Print16DecD (n)
  211. ; p1: n             number    r8      
  212.  
  213. align 16
  214. export Print16DecD:
  215.  
  216.   sub rsp, 18h
  217.  
  218.   mov [rsp+00h], rsi          ; save (x) for String16
  219.   mov [rsp+08h], rdi          ; save (y) for String16
  220.   mov [rsp+10h], rbx
  221.  
  222. ; ----- ENTER -----
  223.  
  224.  
  225.   lea edi, textBuffer + 15    ; Point RDI to the last byte of the buffer
  226.   mov B[edi], 0               ; Null-terminate the string
  227.   dec edi                     ; Move to the previous byte
  228.   mov ebx, 10                 ; Divisor for base-10 conversion
  229.   mov eax, r8d                ; number to convert (can be any unsigned 32-bit value)
  230.  
  231. ; Handle the special case of 0
  232.   test eax, eax
  233.   jnz > convert_loop16
  234.   mov B[edi], '0'
  235.   jmp > donep16
  236.  
  237. convert_loop16:
  238.   xor edx, edx                ; Clear EDX for division (EDX:EAX / EBX)
  239.   div ebx                     ; EAX = EAX / 10, EDX = remainder (0-9)
  240.   add dl, '0'                 ; Convert remainder to ASCII
  241.   mov [edi], dl               ; Store ASCII digit
  242.   dec edi                     ; Move backward in buffer
  243.   test eax, eax
  244.   jnz < convert_loop16        ; Repeat if quotient is not zero
  245.  
  246. donep16:
  247.   inc edi                     ; RDI now points to the first digit
  248. ; At this point, RDI points to the start of the null-terminated string in textBuffer
  249.  
  250. ; Print the string
  251.   mov r8d, edi                ; ptr to number string
  252.   call String16               ; print the string
  253.  
  254.  
  255. ; ----- EXIT -----
  256.  
  257.   mov rsi, [rsp+00h]
  258.   mov rdi, [rsp+08h]
  259.   mov rbx, [rsp+10h]
  260.  
  261.   add rsp, 18h                ; fix the stack
  262.   ret
  263.  
  264. ; ========================================================================================
  265.  
  266. ; Print16HexB (n)
  267. ; p1: n             r8
  268.  
  269. align 16
  270. export Print16HexB:
  271.  
  272.   call dw2hex                 ; convert to an ascii string
  273.  
  274.   lea r8d, textBuffer+6       ; address of the number as a text string
  275.   call String16               ; print the number
  276.  
  277.   ret
  278.  
  279. ; ========================================================================================
  280.  
  281. ; Print16HexW (n)
  282. ; p1: n             r8
  283.  
  284. align 16
  285. export Print16HexW:
  286.  
  287.   call dw2hex                 ; convert to an ascii string
  288.  
  289.   lea r8d, textBuffer+4       ; address of the number as a text string
  290.   call String16               ; print the number
  291.  
  292.   ret
  293.  
  294. ;=========================================================================
  295.  
  296. ; Print16HexD (n)
  297. ; p1: n             r8
  298.  
  299. align 16
  300. export Print16HexD:
  301.  
  302.   call dw2hex                 ; convert to an ascii string
  303.  
  304.   lea r8d, textBuffer         ; address of the number as a text string
  305.   call String16               ; print the number
  306.  
  307.   ret
  308.  
  309. ;=========================================================================
  310.  
  311. ; Print16BinB (n)
  312. ; p1: n             r8
  313.  
  314. align 16
  315. export Print16BinB:
  316.  
  317.   call dw2bin                 ; convert to an ascii string
  318.  
  319.   lea r8d, textBuffer+24      ; address of the number as a text string
  320.   call String16               ; print the number
  321.  
  322.   ret
  323.  
  324. ;=========================================================================
  325.  
  326. ; Print16BinW (n)
  327. ; p1: n             r8
  328.  
  329. align 16
  330. export Print16BinW:
  331.  
  332.   call dw2bin                 ; convert to an ascii string
  333.  
  334.   lea r8d, textBuffer+16      ; address of the number as a text string
  335.   call String16               ; print the number
  336.  
  337.   ret
  338.  
  339. ;=========================================================================
  340.  
  341. ; Print16BinD (n)
  342. ; p1: n             r8
  343.  
  344. align 16
  345. export Print16BinD:
  346.  
  347.   call dw2bin                 ; convert to an ascii string
  348.  
  349.   lea r8d, textBuffer         ; address of the number as a text string
  350.   call String16               ; print the number
  351.  
  352.   ret
  353.  
  354. ;=========================================================================
  355.  
  356.  
Tags: asm x86-64
Advertisement