x86masmvideo-memory

Masm: Writing directly in video memory in ax=12h, int 10h 640*480 16 color vga mode


This is my function of print a dot(cx, dx) using video memory. After calculate the offset di, I don't know how to change the color of the dot, so I ask gpt. Gpt gives me this way but it only shows white dot. How can I generate dots of different color?

vmem1 PROC
    mov ax, 0A000h ; video memory start point
    mov es, ax

    ;offset=y*(640/8)+(x/8)
    mov ax, dx
    mov bx, 80
    mul bx
    mov di, ax

    mov ax, cx
    mov bx, 8
    div bx
    add di, ax ; di=offset
    
    mov dx, 3c4h
    mov al, 2  
    out dx, al
    inc dx
    mov al, 2
    out dx, al

    mov al, 4    
    mov es:[di], al

    ret
    
vmem1 ENDP

Now I can only show a white dot. I hope to generate other colors.


Solution

  • Where you have:

        mov al, 4    
        mov es:[di], al
    

    where you should have:

        mov al,es:[di]
        mov al, 4    
        mov es:[di], al
    

    The "dummy" read loads the latch register as described at http://www.osdever.net/FreeVGA/vga/vgamem.htm#read

    The default palette assigns RED to index 4.

    I would not put my faith in the rest of the code either to be honest, it is not clear to me that the manipulation of the map mask register is necessary or correct. The bit mask register on the other hand is clearly necessary to write a single pixel without affecting adjacent pixels.

    Real examples of such operations are numerous and no harder to find and, I suggest, more reliable and better documented than AI generated nonsense. For example the putpixel example at https://www.fysnet.net/modex.htm for example:

    ; on entry X,Y = location and C = color (0-15)
    putpixel   proc near uses ax bx cx dx
    
    ; byte offset = Y * (horz_res / 8) + int(X / 8)
    
               mov  ax,Y                    ; calculate offset
               mov  dx,80                   ;
               mul  dx                      ; ax = y * 80
               mov  bx,X                    ;
               mov  cl,bl                   ; save low byte for below
               shr  bx,03                   ; div by 8
               add  bx,ax                   ; bx = offset this group of 8 pixels
    
               mov  dx,03CEh                ; set to video hardware controller
    
               and  cl,07h                  ; Compute bit mask from X-coordinates
               xor  cl,07h                  ;  and put in ah
               mov  ah,01h                  ;
               shl  ah,cl                   ;
               mov  al,08h                  ; bit mask register
               out  dx,ax                   ;
    
               mov  ax,0205h                ; read mode 0, write mode 2
               out  dx,ax                   ;
               
               mov  al,es:[bx]              ; load to latch register
               mov  al,Color
               mov  es:[bx],al              ; write to register
    
               ret
    putpixel   endp
    
    X          dw 00h
    Y          dw 00h
    Color      db 00h
    

    Also you would do well to understand the VGA hardware in detail (so at least you might understand what the AI did - and when it doesn't work, why). It is well documented at http://www.osdever.net/FreeVGA/vga/vga.htm for example.