ASM Snippets

 avatar
unknown
assembly_x86
a year ago
5.9 kB
6
Indexable
.model tiny
.486
.data
filen db "file123.txt", 0
handle dw ? 
write db "sidk", 0ah, "ah4261"
rect macro strow, stcol, endrow, endcol, att, a, b
;starting row number in dx
;logic uses displays graphic row by row
        mov     dx,strow
;starting column in cx
b:      mov     cx,stcol
;display one pixel
a:      mov     al,att
        mov     ah,0ch
        int     10h
;increment pixel column number and check whether end column reached
;if no go to the next column
        inc     cx
        cmp     cx,endrow
        jnz     a
;if all columns in a row display has been completed then 
;increment row number - check if last row is reached
;if reached display completed
;if not go on to stcolumn in the next row and display
        inc     dx
        cmp     dx,endrow
        jnz     b
	endm
.code
.startup
	mov ah, 3ch
	lea dx, filen
	mov cl, 20h
	int 21h
	mov handle, ax
	;create a file with file name in filen
	;handle given in handle
	mov ah, 3dh
	mov al, 2h
	lea dx, filen
	int 21h
	mov handle, ax
	;open existing file 
	mov ah, 40h
	mov bx, handle
	mov cx, 11
	lea dx, write
	int 21h
	;write to a file with handle handle
	;number of bytes to write in cx
	;dx will have the address of what you want to write
	;ax contains bytes written
	mov ah, 42h
	mov al, 0
	mov bx, handle
	mov cx, 0
	mov dx, 0
	int 21h
	;move file pointer
	;al = 0 relative to beginning
	;al = 1 relative to current
	;al = 2 relative to end
	mov ah, 3fh
	mov bx, handle
	mov cx, 7
	lea dx, buf
	int 21h
	;this will read the file to address pointed by dx
	;cx = number of bytes to read
	;ds:dx = memory buffer address
	;this is writing to buf, make sure file pointer is not at the end of the file
	mov ah, 3eh
	mov bx, handle
	int 21h
	;close a file with handle in bx
	
	;this will take input of one character into the al register
	mov ah, 01h ;al = 08h for input without echo
	int 21h
	
	;this will input string
	;data has to be in the following format
	;max1 db xx this will store the max number of chars user can type (max 255)
	;act1 db ? this will be actual number of chars user has typed including enter
	;inp1 db 32 dup (?) reserve 32 locations for input
	lea dx, max1
	mov ah, 0ah
	int 21h
	
	;output a character
	mov dl, 'a'
	mov ah, 02h
	int 21h
	
	;output a string (str1 has to be '$' terminated)
	lea dx, str1
	mov ah, 09h
	int 21h
	
	;get display mode
	mov ah, 0fh
	int 10h
	;output:
	;al = current video mode
	;ah = number of character columns
	;bh = page number
	
	;set display mode
	mov ah, 00h
	mov al, 00h ;put display mode in al
	int 10h
	;make sure to go back to original display mode in the end
	;00h - text mode. 40x25. 16 colours. 8 pages. this is column x row
	;03h - text mode. 80x25. 16 colours. 8 pages. this is column x row
	;12h - graphical mode. 80x25. 256 colours. 720x400 pixels. 1 page.
	;even though it's 8 pages, we can only use the first page
	;use graphics mode to display patterns
	
	;get cursor position and size
	mov ah, 03h
	mov bh, 0 ;this is page number, usually 0
	int 10h
	;output:
	;DH = row.
	;DL = column.
	;CH = cursor start line.
	;CL = cursor bottom line.                       
	
	;set cursor position
	mov ah, 02h
	mov dh, 40 ;row
	mov dl, 12 ;column
	mov bh, 0 ;this is page unmber, usually 0
	int 10h
	
	;set cursor size
	mov ah, 01h
	mov ch, ;cursor start line (bits 0-4) and options (bits 5-7).
	mov cl, ;bottom cursor line (bits 0-4).
	int 10h
	
	;read character at cursor position
	mov ah, 08h
	mov bh, 0 ;page number
	int 10h
	;output:
	;ah = attribute
	;al = character
	;The attribute byte is used to specify the foreground and background of the character displayed on the screen.
	;Bits 2-1-0 represent the foreground colour
	;Bit 3 represents the intensity of foreground colour (0-low , 1- high intensity)
	;Bits 6-5-4 represent the background colour
	;Bit 7 is used for blinking text if set to 1
	;000       -black (gray)
	;001       -blue (bright blue)
	;010       -green (bright green)
	;011       -cyan (bright cyan) 
	;100       -red   (bright red)
	;101       -magenta (bright magenta)
	;110       -brown (yellow)
	;111       -white (bright white)

	;write a character at cursor position
	mov ah, 09h
	mov al, ;character to display, this can be space if you just want to color the display ascii - 20h
	mov bh, ;page number
	mov bl, ;attribute
	mov cx, ;number of times to write the character, moving 13*80 (in disp mode 3) will fill upper 13 rows (0-12)
	int 10h
	;make sure to take user input without echo so to continue with our colors else os goes back to default font
	;mov ah, 07h
	;int 21h
	;data is stored in al
	
	;fill a pixel
	mov ah, 0ch
	mov al, ;color
	mov cx, ;column
	mov dx, ;row
	
	;this will print all the text inside buf
	mov cnt, 765
	lea si, buf
x2:	mov al, [si]
	inc si
	mov ah, 09h
	mov bh, 0;page number
	mov bl, attr;attribute
	mov cx, 1;number of times to write the character, moving 13*80 (in disp mode 3) will fill upper 13 rows (0-12)
	int 10h
	mov ah, 02h
	mov dh, row ;row
	mov dl, col ;column
	mov bh, 0 ;this is page unmber, usually 0
	int 10h
	inc col
	cmp col, 79
	je x3
	dec cnt
	jnz x2
	jmp x4
x3: mov col, 0
	inc row
	cmp row, 24
	je x4
	dec cnt
	jnz x2
x4:
	
	;to hold display, system retains programmed display mode
	;this has to be before .exit
	mov ah, 07h
x1:	int 21h
	cmp al, '%'
	jnz x1
.exit
;this will display the count from a word in memory called cocnt
display proc near
	mov ax,cocnt
	mov cl,64h
	div cl
	mov bh,ah
	add al,30h
	mov dl,al
	mov ah,02
	int 21h
	mov cl,0ah
	mov ax,0
	mov al,bh
	div cl
	add al,30h
	mov dl,al
	mov bh,ah
	mov ah,02
	int 21h
	add bh,30h
	mov dl,bh
	mov ah,02
	int 21h
	ret
display endp
end
Editor is loading...
Leave a Comment