flat assembler
Message board for the users of flat assembler.

Index > DOS > How does this code work?

Author
Thread Post new topic Reply to topic
adroit



Joined: 21 Feb 2010
Posts: 252
adroit 13 Mar 2010, 16:03
Hey all, could someone explain to me how does this code works (line-by-line if you could). It was written by Tomasz Grysztar at [content deleted].
It's so cool, that you can draw text from string data, and I want to find how it works.

Code:

       org     100h 

   mov     ax,13h 
     int     10h 

    mov     ax,1130h 
   mov     bh,6 
       int     10h 
        mov     word [font_data],bp 
        mov     word [font_data+2],es 
      push    0A000h 
     pop     es 
 mov     di,320*64+48 
       mov     si,_hello 
  draw_text: 
     lodsb 
      test    al,al 
      jz      done 
       push    ds si 
      lds     si,[font_data] 
     xor     ah,ah 
      shl     ax,4 
       add     si,ax 
      mov     bx,16 
   draw_character: 
   mov     dl,[si] 
    mov     cx,8 
    draw_row: 
 shl     dl,1 
       jnc     next_point 
 mov     ax,cx 
      sub     ax,bx 
      and     ax,1111b 
   add     ax,29 
      mov     byte [es:di+321],al 
    mov     byte [es:di],al 
      next_point: 
      add     di,2 
       loop    draw_row 
   inc     si 
 add     di,(320-8)*2 
       dec     bx 
 jnz     draw_character 
     sub     di,320*2*16-(8+1)*2 
        pop     si ds 
      jmp     draw_text 
  done: 
  xor     ah,ah 
      int     16h 
        mov     ax,3 
       int     10h 
        ret 

_hello db 'Hello, World!',0 

font_data dd ?
    


N.B: It would be really helpful if you could use comment lines to explain what each line does, and still provide with some information on how the code works.
Thanks! Very Happy

_________________
meshnix
Post 13 Mar 2010, 16:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20289
Location: In your JS exploiting you and your system
revolution 13 Mar 2010, 16:18
MeshNix: You can use a debugger and follow the code step by step.
Post 13 Mar 2010, 16:18
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 13 Mar 2010, 16:35
revolution, somewhat hard, the program enters in 320x200 graphics mode.

If someone couldn't see what it does (because of 64-bit limitations), check the attachment.


Description:
Filesize: 11.61 KB
Viewed: 8298 Time(s)

HelloWordByTomasz.PNG


Post 13 Mar 2010, 16:35
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20289
Location: In your JS exploiting you and your system
revolution 13 Mar 2010, 17:00
When debugging we don't have to enter different graphics modes. Just skip over (nop out) the graphics specifics calls and see how the program flow works. We don't have to actually plot the pixels to see how the code works.
Post 13 Mar 2010, 17:00
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 13 Mar 2010, 19:06
I was bored:
Code:
;Interrupt info from Ralph Brown's Interrupt list
        org     100h                     ;Make it a .com file

        mov     ax,13h                   ;Mode 13h, 320x200 256 colors
        int     10h                      ;^^^^

        mov     ax,1130h                 ;GET FONT INFORMATION (EGA, MCGA, VGA)
        mov     bh,6                     ;06h ROM 8x16 font (MCGA, VGA)

        int     10h                      ;Return: ES:BP = specified pointer
                                         ;Also returned, but not used here:
                                         ;CX    = bytes/character of on-screen font (not the requested font!)
                                         ;DL    = highest character row on screen


        mov     word [font_data],bp      ;Fill the variable with the pointer to the specified font(8x16).
        mov     word [font_data+2],es    ;^^^^^^^^

        push    0A000h                   ;Setup es for writing to the screen(A000h = start of screen buffer).
        pop     es                       ;^^^^
        mov     di,320*64+48             ;Start location to start writing pixels. 64 lines down and 48 across.
        mov     si,_hello                ;Set si up with location of the "Hello, World!" string.
  draw_text: 
        lodsb                            ;Load a byte of the string into al.
        test    al,al                    ;Is it zero?
        jz      done                     ;If so, we're done.
        push    ds si                    ;If not, push ds and si
        lds     si,[font_data]           ;Load DS:SI the font data SEGMENT:OFFSET.So now DS points to the font segment, and SI is the offset.
        xor     ah,ah                    ;zero out the top of ax
        shl     ax,4                     ;Multiply the current byte of the string by 16(hint, the font is 8x16, stored as 8 pixels per byte)
        add     si,ax                    ;Add that to the start offset of the font data. SI now points to the start of the pixel data for the current character.
        mov     bx,16 
   draw_character: 
        mov     dl,[si] 
        mov     cx,8 
    draw_row: 
        shl     dl,1 
        jnc     next_point 
        mov     ax,cx                    ;This all just draws it out fancy, I'm too lazy to continue....
        sub     ax,bx 
        and     ax,1111b 
        add     ax,29 
        mov     byte [es:di+321],al 
        mov     byte [es:di],al 
      next_point: 
        add     di,2 
        loop    draw_row 
        inc     si 
        add     di,(320-8)*2 
        dec     bx 
        jnz     draw_character 
        sub     di,320*2*16-(8+1)*2 
        pop     si ds 
        jmp     draw_text 
  done:
        xor     ah,ah                    ;
        int     16h                      ;Wait for a keypress

        mov     ax,3                     ;Set mode 3, the normal console mode
        int     10h                      ;^^^^^^^^^^^
        ret                              ;And....exit.

_hello db 'Hello, World!',0 

font_data dd ?    


Didn't finish it though, got bored of it.



Maybe some non-lazy person could comment the screen printing part.

_________________
----> * <---- My star, won HERE
Post 13 Mar 2010, 19:06
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 14 Mar 2010, 01:26
That's not as impressive after you've seen his other stuff. I found the attached somewhere
buried in the os dev forum. It's tetris in the form of a 512b demo os. It ran in Bochs when I
tried it, use the arrow keys to rotate and move the pieces.


Description: Source and binary of Tetros, by Tomasz Grysztar
Download
Filename: TETROS.ZIP
Filesize: 2.28 KB
Downloaded: 645 Time(s)

Post 14 Mar 2010, 01:26
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit 14 Mar 2010, 04:01
Thanks windwakr!
Tyler, I ran it on QEMU and it is really remarkable! It's a bootable game!!!Shocked
Post 14 Mar 2010, 04:01
View user's profile Send private message Reply with quote
Flat12



Joined: 25 Jan 2013
Posts: 5
Flat12 25 Jan 2013, 20:51
Cool Smile but this code is disk image. How convert code to ISA or PCI option ROM. I know that need 55 AA 01 (1*200) Jump to execute code and probably RET - please help.
Post 25 Jan 2013, 20:51
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.