Chapter 15 examples
description
Transcript of Chapter 15 examples
Chapter 15 examples
BIOS-level programming
Variation on keybd.asm
Keybd2.asm code…
.data;;;I added these messagescharis byte "char is=",0scan byte "scan code=",0ascii byte "ascii code=",0.codemain PROC
mov ax,@datamov ds,axcall ClrScr ; clear screen
L1: mov ah,10h ; keyboard inputint 16h ; using BIOSpush axpush axmov edx, offset chariscall writestringmov dl,alpush axmov ah,2int 21h
exe in p drivecall ClrScr ; clear screenpop axcall crlfand eax,0ff00hmov edx, offset scancall writestringshl ax,8call writeIntcall crlfmov edx, offset asciicall writestringpop axand ax,0ffhcall writeIntcall crlfpop axcmp al,1Bh ; ESC key pressed?jne L1 ; no: repeat the loop
Clearing the type-ahead buffer
• If the user is typing keys in some loop (in a game situation, for example) and the program is waiting for a particular key press, you may need to clear the type-ahead buffer
INCLUDE Irvine16.incClearKeyboard PROTO, scanCode:BYTEESC_key = 1 ; scan code
.codemain PROCL1: ;------------------------- LOOP BODY
; Display a dot, to show program's progressmov ah,2mov dl,'.'int 21hmov eax,300 ; delay for 300 mscall Delay;-------------------------INVOKE ClearKeyboard,ESC_key ; check for Esc keyjnz L1 ; continue loop if ZF=0
quit:call Clrscrexit
main ENDP
clearkbd;---------------------------------------------------ClearKeyboard PROC,
scanCode:BYTE;; Clears the keyboard, while checking for a; particular scan code.; Receives: keyboard scan code; Returns: Zero flag set if the ASCII code is; found; otherwise, Zero flag is clear.;---------------------------------------------------
push axL1:
mov ah,11h ; check keyboard bufferint 16h ; any key pressed?jz noKey ; no: exit now (ZF=0)mov ah,10h ; yes: remove from bufferint 16hcmp ah,scanCode ; was it the exit key?je quit ; yes: exit now (ZF=1)jmp L1 ; no: check buffer again
noKey: ; no key pressedor al,1 ; clear zero flag
quit:pop axret
ClearKeyboard ENDPEND main
Not too impressive
getting keyboard flags directlykbdseg dw 0040hkbdofs dw 17hstring byte 'the keyboard settings',0ah,0dh,0flags word ?.codemain PROC
mov ax,@datamov ds,axmov ax,kbdsegmov es,axmov di,kbdofs
xor eax,eaxmov al, byte ptr ES:[di]inc dimov ah,byte ptr ES:[di]mov flags,axcall writebin
getting keyboard flags indirectly
xor eax,eax
mov ah,12h
int 16h
mov flags,ax
call writebin
Keyboard flags: note 1s for caps and numlock toggle keys (table pg 496)
C:\Masm615\Examples\ch15>FLAGS
0000 0000 0110 0000
C:\Masm615\Examples\ch15>FLAGS2
0000 0000 0110 0000
C:\Masm615\Examples\ch15>
Video display: 3 levels
1. Use DOS int 21h to put stuff on the screen
2. Use BIOS int 10h to put stuff on screen (no output redirect allowed)
3. Direct video access…write chars directly onto screen (see keyboard flags version for example of how to set seg and ofset)
Video using int 10h
• Each color pixel is generated by an electron beam with r-g-b components A fourth channel control intensity, in the format:
• I-R-G-B• Text table pg 500 shows bit setting for various
colors.• In color text mode, each character has an
attribute byte consisting of 2 4-bit color settings for background and foreground. (bottom pg 500)
Int 10h
• Text pg 501 has a list of int 10h function codes
• These include reading characters from the screen, writing chars or pixels onto the screen
Txt pg 502 lists video modes
Mode Resolution colors
0 40X25 1
1 40X25 16
2 80X25 2
3 80X25 16
7 80X25 2
14h 132X25 16
Int 10h functions
• Get current video mode and save it before setting a new video mode:
Int10h, function ah=0 sets mode in al.
• Function 1 sets the cursor size
• Function 2 sets cursor position (dh,dl=row,col; bh=videopage)
Int 10h functions
• Function 3=get curpos & size
• Text has sample routines pg 545 to show/hide the cursor.. Here’s an example:Showcur procMov ah,1Mov cx,0607h;default sizeint 10hRetShowcur endp
Writing (color) text to a window… text example
TextWin.asm (left out a few lines); Scroll a window.
mov ax,0600h ; scroll windowmov bh,00011110b ; yellow on bluemov cx,050Ah ; upper-left cornermov dx,0A30h ; lower-right cornerint 10h
; Position the cursor inside the window.mov ah,2 ; set cursor positionmov dx,0714h ; row 7, col 20mov bh,0 ; video page 0int 10h
; Write some text in the window.mov dx,OFFSET messagecall WriteString
; Wait for a keypress.mov ah,10hint 16hexit
Text example… colorstring main proccall ClrScr
call EnableBlinkingmov cx,SIZEOF stringmov si,OFFSET string
L1: push cx ; save loop countermov ah,9 ; write character/attributemov al,[si] ; character to displaymov bh,0 ; video page 0mov bl,color ; attributeor bl,ATTRIB_HI ; set blink/intensity bitmov cx,1 ; display it one timeint 10hmov cx,1 ; advance cursor tocall AdvanceCursor ; next screen columninc color ; next colorinc si ; next characterpop cx ; restore loop counterLoop L1
call Crlfexit
EnableBlinking PROC
EnableBlinking PROC;; Enable blinking (using the high bit of color; attributes). In MS-Windows, this only works if; the program is running in full-screen mode.; Receives: nothing.; Returns: nothing;--------------------------------------------------
push axpush bxmov ax,1003hmov bl,1 ; blinking is enabledint 10hpop bxpop axret
EnableBlinking ENDP
AdvanceCursor PROC
AdvanceCursor PROC;; Advances the cursor n columns to the right.; Receives: CX = number of columns; Returns: nothing;--------------------------------------------------
pushaL1:
push cx ; save loop countermov ah,3 ; get cursor positionmov bh,0 ; into DH, DLint 10h ; changes CX register!inc dl ; increment columnmov ah,2 ; set cursor positionint 10hpop cx ; restore loop counterloop L1 ; next column
poparet
Run of colorstr
Graphics in int 10h: table of video graphics modes on pg 512
Mode Resolution # colors
6 640X200 2
0dh 320X200 16
0eh 640X200 16
0fh 640X350 2
10h 640X350 16
11h 640X480 2
12h 640X480 16
13h 320X200 256
6ah 800X600 16
functions
• 0Ch write a pixel
• 0dh read a pixel
• See sample calls and parameters in text
Drawline example: Changes mode to full screen and draws a tiny line segment:
INCLUDE Irvine16.inc
;------------ Video Mode Constants -------------------
Mode_06 = 6 ; 640 X 200, 2 colors
Mode_0D = 0Dh ; 320 X 200, 16 colors
Mode_0E = 0Eh ; 640 X 200, 16 colors
Mode_0F = 0Fh ; 640 X 350, 2 colors
Mode_10 = 10h ; 640 X 350, 16 colors
Mode_11 = 11h ; 640 X 480, 2 colors
Mode_12 = 12h ; 640 X 480, 16 colors
Mode_13 = 13h ; 320 X 200, 256 colors
Mode_6A = 6Ah ; 800 X 600, 16 colors
More….datasaveMode BYTE ? ; save the current video modecurrentX WORD 100 ; column number (X-coordinate)currentY WORD 100 ; row number (Y-coordinate)color BYTE 1 ; default color; In 2-color modes, white = 1; In 16-color modes, blue = 1.codemain PROC
mov ax,@datamov ds,ax
; Save the current video modemov ah,0Fhint 10hmov saveMode,al
; Switch to a graphics modemov ah,0 ; set video modemov al,Mode_11int 10h
And the rest; Draw a straight line
LineLength = 100mov dx,currentYmov cx,LineLength ; loop counter
L1:push cxmov ah,0Ch ; write pixelmov al,color ; pixel colormov bh,0 ; video page 0mov cx,currentXint 10hinc currentX;inc color ; try this for multi-color modespop cxLoop L1
; Wait for a keystrokemov ah,0int 16h
; Restore the starting video modemov ah,0 ; set video mode… this part doesn’t seem to workmov al,saveMode ; saved video modeint 10hexit
main ENDP
Cartesian coordinates
• Need to switch command prompt to full screen
• Even so, it worked ok when I used mode11 but not mode6
• Can’t get screen shot in full screen mode
Cartesian example …in slide notes
.datasaveMode BYTE ? ; save the current video modecurrentX WORD 100 ; column number (X-coordinate)currentY WORD 100 ; row number (Y-coordinate)color BYTE 1 ; default color
; In 2-color modes, white = 1; In 16-color modes, blue = 1
.codemain PROC
mov ax,@datamov ds,ax
; Save the current video modemov ah,0Fhint 10hmov saveMode,al
; Switch to a graphics modemov ah,0 ; set video modemov al,Mode_11;;;;;note mode6 did not work for meint 10h
Cartesian example;mov cx,X_axisXmov dx,x_axisYmov ax,x_axislenmov bl,whitecall horizontal
mov cx,y_axisXmov dx,y_axisYmov ax,y_axislenmov bl,whitecall vertical; Wait for a keystroke
mov ah,0int 16h
; Restore the starting video modemov ah,0 ; set video modemov al,saveMode ; saved video modeint 10hexit
main ENDP
Cartesian example: horizontal linehorizontal proc.datacurrx word ?.code
pushamov currx,cxmov cx,ax
H1:push cxmov al,blmov ah,0chmov bh,0 ;;pagemov cx,currxint 10hinc currxpop cxloop h1poparet
horizontal endp
Cartesian example: vertical linevertical proc.datacurry word ?.codepushamov curry,dxmov currx,cxmov cx,axV1:push cxmov al,blmov ah,0chmov bh,0mov cx,currxmov dx,curryint 10hinc currypop cxloop V1poparetvertical endp
END main
Section 15.5 memory mapped graphics
• Mode 13h 320X200, 256 color
• Text example Mode13.asm
• Draws a few blue dots on the screen…
• Need to run this in full screen
boatgame
• Code in slide and posted
• Draws a boat and sub
• Uses old medium resolution graphics mode.
Medium resolution examples
• These examples are based on an older graphics card but they still work:
• Moveball---in slide notes
• Medres plays cellular automata in medium resolution
• Boatgame ---ask for source
moveball
• Draws a cyan ball on a blue screen.
• Hit or hold key to move ball rightwards (it wraps)
• Type ‘x’ to exit
Hr examples from another text: draws a series of lines in hr –very pretty but can’t get screenshot
.model small
.stack 100h
.386
.codemain proc near
mov ax,@datamov ds,axmov es,ax
mov ah,0fhint 10hpush axcall b10modecall c10displaymov ah,10hint 16hpop axmov ah,0int 10hmov ax,4c00hint 21h
main endpb10mode proc near
mov ax,12hint 10hmov ah,0bhmov bx,7int 10hret
b10mode endp
Hr2 continuedc10display proc near
pushaxor bx,bxmov cx,64mov dx,70
@@20: mov ah,0ch mov al,bl int 10h inc cx cmp cx, 576;;;row/col delimiters in cx,dc jne @@20 mov cx,64 inc bl inc dx cmp dx,280 jne @@20 popa retc10display endpend main
Hr4…similar…writes letters .model small.stack 100h.386video segment at 0b900h;;;page 1vid db 1000H dup (?)video ends
.codemain proc near
mov ax,videomov es,axassume es:videomov ah,0fhint 10hpush ax;;;save current modepush bxmov ax,3int 10hmov ax,501h;;page 1int 10hcall b10displaymov ah,10h;;wait for keyint 16h
hr4mov ah,5pop bxmov al,bhint 10hpop axxor ah,ahint 10hmov ax,4c00hint 21h
main endpb10display proc
pushamov al,41hmov ah,1mov di,820
@@10: mov cx,60@@20: mov es:word ptr[di],ax
add di,2loop @@20inc ahinc aladd di,40cmp al,51hjne @@10poparet
b10display endpend main
hr4
Hr5 draws a block in fullscreen dos window – no screen shot.. Code in slide notes
.model small
.stack 100h
.386
.datacolor db ?back dw 50front dw 250cstart dw 50cstop dw 250rstart dw 50rstop dw 250red equ 12grey equ 7.codemain proc near
mov ax,@datamov ds,axmov es,axmov ah,0fhint 10hpush axcall b10mode
Hr5 continued (next slide also)
mov color,redcall putblock;;;code on next slidedown:mov ah,10hint 16hpop axmov ah,0int 10hmov ax,4c00hint 21h
main endp
putblock proc nearpushamov bx,0
mov cx,cstartmov dx,rstart
@@20:;check if point cx,dx is in circlepush cxpush dxsub dx,100sub cx,100mov ax,cximul cxpush axov ax,dximul dxpop dxadd ax,dxpop dxpop cxcmp ax,10000jg @@next
mov al,color mov ah,0ch;;;write dot int 10h@@next:
inc cx cmp cx, cstop jg @@30 jmp @@20 @@30: mov cx,cstart inc dx cmp dx,rstop jg @@40 jmp @@20 @@40: popa retputblock endpend main
More of it
b10mode proc nearmov ax,12hint 10hmov ah,0bhmov bx,7int 10hret
b10mode endp
Other examples
• Game of life (life.exe/.asm) implements a not too interesting one-dimensional cellular automata in hr graphics
• Plotdot3 seems to be the same as hr2