[Assembly] Realize color conversion and draw graphics (.asm source code)

Project environment
  • Compilation environment: build a nasm2.8 compilation environment.
  • Mirror file: use winhex_18.2 to brush in the code.
  • Virtual machine: Bochs-2.4.5 is used .
Project software
operation result

Insert picture description here

Custom color block
;编制自定义颜色
editcolor:
	;设置颜色号接口0x3c8,RGB分量接口0x3c9
	mov dx,0x3c8
	mov al,[si]
	out dx,al

	mov dx,0x3c9
	mov al,[si+1]
	out dx,al

	mov al,[si+2]
	out dx,al

	mov al,[si+3]
	out dx,al

	add si,4

	ret
Program source code (.asm)
org 0x7c00

;CS,DS,ES,SS默认的段基址为0000
;利用jmp指令跳过段数据的定义部分
jmp start

;其中0号默认为背景色
;[1,+]为前景色
;XXX,笔画数为8,8,12,取公倍数160,160,240
;si
color:	db 0,255,255,255 
		db 2,0,0,0
	  
;bx
startP: dw 0 
		dw 319
		dw 63680
		dw 0
;ax
endP:	dw 319
		dw 63999
		dw 63999
		dw 63680
;主函数入口地址
start:	
	;进入13320*200 256色的图形模式
	mov ah,00h
	mov al,13h
	int 10h

	mov si,color;赋值底色
	call editcolor
	mov si,color;赋值黑色
	call editcolor

	;0xb800为显存的段基址
	mov ax,0xa000
	mov es,ax

	;整体框线1-----
	mov bx,[ds:startP]
    mov ax,[ds:endP]
    call DrawLineOfH
	;整体框线2|
	mov bx,[ds:startP]
    mov ax,[ds:endP]
    call DrawLineOfV
	;整体框线3-----
	mov bx,[ds:startP]
    mov ax,[ds:endP]
    call DrawLineOfH
	;整体框线4|
	mov bx,[ds:startP]
    mov ax,[ds:endP]
    call DrawLineOfV
e:	jmp $

;编制自定义颜色
editcolor:
	;设置颜色号接口0x3c8,RGB分量接口0x3c9
	mov dx,0x3c8
	mov al,[si]
	out dx,al

	mov dx,0x3c9
	mov al,[si+1]
	out dx,al

	mov al,[si+2]
	out dx,al

	mov al,[si+3]
	out dx,al

	add si,4

	ret

;横线
DrawLineOfH:
	cmp bx,ax
	ja DrawLineOfHExit
	mov byte [es:bx], 2
	inc bx
	jmp DrawLineOfH
DrawLineOfHExit:
	mov cx,word[ds:startP+2]
	mov word[ds:startP],cx
	mov cx,word[ds:endP+2]
	mov word[ds:endP],cx
	ret
;竖线
DrawLineOfV:
	cmp bx,ax
	ja DrawLineOfVExit
	mov byte [es:bx], 2
	add bx,320
	jmp DrawLineOfV
DrawLineOfVExit:
	mov cx,word[ds:startP+2]
	mov word[ds:startP],cx
	mov cx,word[ds:endP+2]
	mov word[ds:endP],cx
	ret

Guess you like

Origin blog.csdn.net/Gyangxixi/article/details/113613006