I am trying to launch procedure put pixel in assembly in graphic mode on DOS NASM. When I don't use procedure it work, but now it show me only one blue pixel and two letters "h" in the left corner. My code:
segment .data
segment .code
..start:
mov ax, 13h
int 10h
mov ax, 0a000h ; The offset to video memory
mov es, ax ; We load it to ES through AX, becouse immediate operation is not allowed on ES
;;;;;;;;;;;;;;;;;;;;;;
mov ax, 10 ; Y coord
mov bx, 20 ; X coord
mov dl, 4
call putpixel
mov ax, 1 ; Y coord
mov bx, 2 ; X coord
mov dl, 4
call putpixel
;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;
putpixel:
mov cx,320
mul cx; multiply AX by 320 (cx value)
add ax,bx ; and add X
mov di,ax
mov [es:di],dl
ret
;;;;;;;;;;;;;;;;;;;;;;
xor ah, ah
int 16h ;keyboard
mov ax, 3
int 10h ; go to text mode
mov ax, 4c00h
int 21h
I got it from tutorial, but I have any idea why it doesn't work. What is wrong in this code? I was trying to show two pixels. Thanks for help :)
Your code works properly to set the two pixels, but then falls into the putpixel
subroutine, causing havoc.
I've rearranged your code so that it finishes properly. Either put your subroutines last, or first, or jmp around them. (I generally go with first, so that the main logic (start, main, whatever) is the last of the source code. Your choice, of course)
segment .data
segment .code
..start:
mov ax, 13h
int 10h ; switch to 320x200 mode
mov ax, 0a000h ; The offset to video memory
mov es, ax ; We load it to ES through AX,
; because immediate operation
; is not allowed on ES
;;;;;;;;;;;;;;;;;;;;;;
mov ax, 10 ; Y coord
mov bx, 20 ; X coord
mov dl, 4
call putpixel
mov ax, 1 ; Y coord
mov bx, 2 ; X coord
mov dl, 4
call putpixel
;;;;;;;;;;;;;;;;;;;;;;;;;
xor ah, ah
int 16h ; keyboard (wait for key)
mov ax, 3
int 10h ; go to text mode
mov ax, 4c00h
int 21h ; return to DOS, exit code 0
;;;;;;;;;;;;;;;;;;;;;
putpixel:
push dx ; oops, mul changes dx too
mov cx, 320
mul cx ; multiply Y (ax) by 320 (one row)
add ax, bx ; and add X (bx) (result= dx:ax)
mov di, ax
pop dx
mov [es:di], dl ; store color/pixel
ret
;;;;;;;;;;;;;;;;;;;;;;
Congratulations on tackling assembly language! It's not always easy, but I find it amazing and fun to explore! Good luck!
EDIT:
Oops! Totally forgot that the mul
command affects DX
.
mul cx --> dx:ax = ax * cx (oops!)