Il post here some code pieces i want to share with you.
These code pieces are not ment to be fast, so they are not optimised fore speed.
The only thing thet i do offten is to program with 32Byte stuck frames because i am programing for the Pentium MMX.
Its cachelines are 32Byte long. So if you align your stuck on 32Bytes you may gain some speed.
So here you are:
This is for printing zero terminated strings on the screen:
Code: Select all
%ifndef __print__
%define __print__
;== Description ================================================================
;
; This is a real mode program for printing zero terminated ASCII-Strings on the
; screen with the BIOS.
;
;== Includes ===================================================================
;== Defines ====================================================================
;== Code =======================================================================
[segment .text]
;-------------------------------------------------------------------------------
;
; print Size: 44 Bytes
;
;
; Brief:
; This is a real mode program for printing zero terminated ASCII-Strings on the
; screen with the BIOS.
;
; Prototype:
; nothing print (char* String)
;
; Parameter:
; char* String = String address
;
; Return value:
; nothing
;
; Description:
; This is a real mode program for printing zero terminated ASCII-Strings on the
; screen with the BIOS.
; This programm does not affect any of the registers. No return value in AX,
; AX is not changed.
; We do not trust the BIOS, so we save the environment on the stuck.
;
; Callingconvention:
; You have to push the address of the string and then call the program.
; Example:
;
; push String
; call print
;
print:
pushf
pusha
push DS
push ES
push FS
push GS
mov BP, SP
sub SP, 2
; Our stuck-frame:
;
; Our lokal variables and some other things will be put on this
; stuck-frame.
;
; 30 word[BP + 1Ch] = char* String
; 28 word[BP + 1Ah] = IP
; 26 word[BP + 18h] = Safed environment FLAGS
; 24 word[BP + 16h] = Safed environment AX
; 22 word[BP + 14h] = Safed environment CX
; 20 word[BP + 12h] = Safed environment DX
; 18 word[BP + 10h] = Safed environment BX
; 16 word[BP + 0Eh] = Safed environment SP
; 14 word[BP + 0Ch] = Safed environment BP
; 12 word[BP + 0Ah] = Safed environment SI
; 10 word[BP + 08h] = Safed environment DI
; 08 word[BP + 06h] = Safed environment DS
; 06 word[BP + 04h] = Safed environment ES
; 04 word[BP + 02h] = Safed environment FS
; 02 <BP word[BP + 00h] = Safed environment GS
; 00 <SP
mov SI, word[BP + 1Ch]
jmp .print
.BIOS_Call:
mov BX, 0001h
mov AH, 0Eh
int 10h
.print:
lodsb
cmp AL, 00h
jne .BIOS_Call
.end:
add SP, 2
pop GS
pop FS
pop ES
pop DS
popa
popf
ret 2
%endif
Code: Select all
%ifndef __fdd__
%define __fdd__
;== Description ================================================================
;
; This is a real mode program for reading sectors from the floopy disk.
;
;== Includes ===================================================================
;== Defines ====================================================================
;== Code =======================================================================
[segment .text]
;-------------------------------------------------------------------------------
;
; FDD_Reset Size: 43 Bytes
;
;
; Brief:
; Floppy disk drive reset.
;
; Prototype:
; byte FDD_Reset (word Drive)
;
; Parameter:
; word Drive = Nomber of the drive to be reseted.
;
; Return value:
; AL Error Code
;
; Description:
; This funktion reset's the FDD. If the reset faild, the funktion returns an
; errorcode.
; This program can also be yoused for hard disks.
;
; Posible errorcodes are:
;
; 00h successful completion
; 01h invalid function in AH or invalid parameter
; 02h address mark not found
; 03h disk write-protected
; 04h sector not found/read error
; 05h reset failed (hard disk)
; 05h data did not verify correctly (TI Professional PC)
; 06h disk changed (floppy)
; 07h drive parameter activity failed (hard disk)
; 08h DMA overrun
; 09h data boundary error (attempted DMA across 64K boundary or >80h
; sectors)
; 0Ah bad sector detected (hard disk)
; 0Bh bad track detected (hard disk)
; 0Ch unsupported track or invalid media
; 0Dh invalid number of sectors on format (PS/2 hard disk)
; 0Eh control data address mark detected (hard disk)
; 0Fh DMA arbitration level out of range (hard disk)
; 10h uncorrectable CRC or ECC error on read
; 11h data ECC corrected (hard disk)
; 20h controller failure
; 31h no media in drive (IBM/MS INT 13 extensions)
; 32h incorrect drive type stored in CMOS (Compaq)
; 40h seek failed
; 80h timeout (not ready)
; AAh drive not ready (hard disk)
; B0h volume not locked in drive (INT 13 extensions)
; B1h volume locked in drive (INT 13 extensions)
; B2h volume not removable (INT 13 extensions)
; B3h volume in use (INT 13 extensions)
; B4h lock count exceeded (INT 13 extensions)
; B5h valid eject request failed (INT 13 extensions)
; B6h volume present but read protected (INT 13 extensions)
; BBh undefined error (hard disk)
; CCh write fault (hard disk)
; E0h status register error (hard disk)
; FFh sense operation failed (hard disk)
;
; Callingconvention:
; You have to push the number of the drive and then call the program.
; Example:
;
; push DriveNr
; call FDD_Reset
;
FDD_Reset:
pushf
pusha
push DS
push ES
push FS
push GS
mov BP, SP
sub SP, 2
; Cacheline
; 30 word[BP + 1Ch] = Drive
; 28 word[BP + 1Ah] = IP
; 26 word[BP + 18h] = Safed environment FLAGS
; 24 word[BP + 16h] = Safed environment AX
; 22 word[BP + 14h] = Safed environment CX
; 20 word[BP + 12h] = Safed environment DX
; 18 word[BP + 10h] = Safed environment BX
; 16 word[BP + 0Eh] = Safed environment SP
; 14 word[BP + 0Ch] = Safed environment BP
; 12 word[BP + 0Ah] = Safed environment SI
; 10 word[BP + 08h] = Safed environment DI
; 08 word[BP + 06h] = Safed environment DS
; 06 word[BP + 04h] = Safed environment ES
; 04 word[BP + 02h] = Safed environment FS
; 02 <BP word[BP + 00h] = Safed environment GS
; 00 <SP
.BIOS_Call:
mov AH, 00h
mov DL, byte[BP + 1Ch]
int 13h
mov byte[BP + 16h], 0
jnc .end
.error:
mov byte[BP + 16h], AH
.end:
add SP, 2
pop GS
pop FS
pop ES
pop DS
popa
popf
ret 2
;===============================================================================
;
; FDD_Read_Sector Size: 79 Bytes
;
;
; Brief:
; Read's one ore more Sectores from the FDD.
;
; Prototype:
; byte FDD_Read_Sector (word Drive, word Cylinder, word Head, word Sector,
; word SectorCount, word SegAdr, word OffAdr)
;
; Parameter:
; word Drive = Drive to read from.
; word Cylinder = The Cylindernumber. Cylindernumbers from 0 to 79 are
; falid for the FDD.
; word Head = The Headnumber. Headnumbers from 0 to 1 are falid for
; the FDD.
; word Sector = The Sectornumber. Sectornumbers from 1 to 18 are
; falid for the FDD.
; word SectorCount = Number of Sectors to read. This falue depends on witch
; sector you read. If you read Sector 4 the maximum for
; SectorCount is 15. If you read Sector 18 the maximum
; for SectorCount is 1. If you read Sector 1 the maximum
; for SectorCount is 18.
; word SegAdr = SegAdr and OffAdr are the Memory-Address.
; word OffAdr = SegAdr and OffAdr are the Memory-Address.
;
; Return value:
; AL Error Code
;
; Description:
; This funktion read's one ore more sectors from the FDD. If the read faild
; the funktion returns an errorcode.
; This program can also be yoused for hard disks.
;
; Posible errorcodes are:
;
; 00h successful completion
; 01h invalid function in AH or invalid parameter
; 02h address mark not found
; 03h disk write-protected
; 04h sector not found/read error
; 05h reset failed (hard disk)
; 05h data did not verify correctly (TI Professional PC)
; 06h disk changed (floppy)
; 07h drive parameter activity failed (hard disk)
; 08h DMA overrun
; 09h data boundary error (attempted DMA across 64K boundary or >80h
; sectors)
; 0Ah bad sector detected (hard disk)
; 0Bh bad track detected (hard disk)
; 0Ch unsupported track or invalid media
; 0Dh invalid number of sectors on format (PS/2 hard disk)
; 0Eh control data address mark detected (hard disk)
; 0Fh DMA arbitration level out of range (hard disk)
; 10h uncorrectable CRC or ECC error on read
; 11h data ECC corrected (hard disk)
; 20h controller failure
; 31h no media in drive (IBM/MS INT 13 extensions)
; 32h incorrect drive type stored in CMOS (Compaq)
; 40h seek failed
; 80h timeout (not ready)
; AAh drive not ready (hard disk)
; B0h volume not locked in drive (INT 13 extensions)
; B1h volume locked in drive (INT 13 extensions)
; B2h volume not removable (INT 13 extensions)
; B3h volume in use (INT 13 extensions)
; B4h lock count exceeded (INT 13 extensions)
; B5h valid eject request failed (INT 13 extensions)
; B6h volume present but read protected (INT 13 extensions)
; BBh undefined error (hard disk)
; CCh write fault (hard disk)
; E0h status register error (hard disk)
; FFh sense operation failed (hard disk)
;
; Callingconvention:
; You have to push drive number, cylinder number, head number, sector number,
; SectorCount, the segment address and the offset address and then call the
; program.
; Example:
;
; push DriveNr
; push CylinderNr
; push HeadNr
; push SectorNr
; push SectorCount
; push SegAdr
; push OffAdr
; call FDD_Read_Sector
;
FDD_Read_Sector:
pushf
pusha
push DS
push ES
push FS
push GS
mov BP, SP
sub SP, 22
; Stack Cacheline
; 30 word[BP + 28h] = Drive
; 28 word[BP + 26h] = Cylinder
; 26 word[BP + 24h] = Head
; 24 word[BP + 22h] = Sector
; 22 word[BP + 20h] = SectorCount
; 20 word[BP + 1Eh] = SegAdr
; 18 word[BP + 1Ch] = OffAdr
; 16 word[BP + 1Ah] = IP
; 14 word[BP + 18h] = Safed environment FLAGS
; 12 word[BP + 16h] = Safed environment AX
; 10 word[BP + 14h] = Safed environment CX
; 08 word[BP + 12h] = Safed environment DX
; 06 word[BP + 10h] = Safed environment BX
; 04 word[BP + 0Eh] = Safed environment SP
; 02 word[BP + 0Ch] = Safed environment BP
; 00 word[BP + 0Ah] = Safed environment SI
; 30 word[BP + 08h] = Safed environment DI
; 28 word[BP + 06h] = Safed environment DS
; 26 word[BP + 04h] = Safed environment ES
; 24 word[BP + 02h] = Safed environment FS
; 22 <BP word[BP + 00h] = Safed environment GS
; 20 word[BP - 02h] = TMP CX
; 18
; 16
; 14
; 12
; 10
; 08
; 06
; 04
; 02
; 00 <SP
.BIOS_Call:
mov AH, 02h
mov DL, byte[BP + 28h] ; Drive
mov CH, byte[BP + 26h] ; Lower 8 bits of Cylinder
mov DH, byte[BP + 24h] ; Head
; Sector/Cylinder Calculation
mov CL, byte[BP + 27h] ; Higher 8 bits of Cylinder
and CL, 00000011b
shl CL, 6
mov byte[BP - 02h], CL
mov CL, byte[BP + 22h] ; Sector
and CL, 00111111b
or CL, byte[BP - 02h] ; Sector/Cylinder
mov AL, byte[BP + 20h] ; SectorCount
mov ES, word[BP + 1Eh] ; Segment Address
mov BX, word[BP + 1Ch] ; Offset Address
int 13h
mov byte[BP + 16h], 0
jnc .end
.error:
mov byte[BP + 16h], AH
.end:
add SP, 22
pop GS
pop FS
pop ES
pop DS
popa
popf
ret 14
%endif
feel free to ask questions
Alex