Code i want to share

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Alex
Posts: 16
Joined: Sun Jan 18, 2009 2:12 am

Code i want to share

Post by Alex »

Hi people :)

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

This is for reading from the floopy disk ore hard disk:

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
I use NASM to assemble the code and my text editor is "Programer's Notepad 2".

feel free to ask questions :)

Alex
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Code i want to share

Post by bluemoon »

It's good that you actually write more line of comment then the code. I mean, it's really a habit not many programmer can do.

I have no comment about the code itself.
Alex
Posts: 16
Joined: Sun Jan 18, 2009 2:12 am

Re: Code i want to share

Post by Alex »

@bluemoon: thank you :)

And here a nother code piece :D

It is for converting a dword into a hex, dec or binary string:

Code: Select all

%ifndef __dw2ASCII__
%define __dw2ASCII__

;== Description ================================================================
;
;  This program converts a byte, word or dword into a ASCII-String.
;
;  The ASCII-String is a fixed size ASCII-String. Its length is 34 bytes.
;  The structure is as follows:
;
;  snnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnx
;
;  x = h for hex, d for dec, b for bin
;  n = Number
;  s = Sign
;
;  The "s" part is at the lowest address and the "x" part is at the highest.
;

;== Includes ===================================================================

    

;== Defines ====================================================================

    %define MaxDigitSize 32

    struc dw2ASCII_String
        
        .s  resb 1              ; Sign
        .n  resb MaxDigitSize   ; Digits
        .x  resb 1              ; h for hex, d for dec, b for bin
        
    endstruc


;== Code =======================================================================
[segment .text]

;-------------------------------------------------------------------------------
;
;  dw2uhex                                                         Size: n Bytes
;
;
;  Brief:
;   Converts the unsigned doubleword into a unsigned hexadecimal number and
;   stores it into a string.
;
;  Prototype:
;   nothing dw2uhex (dword Number, dw2ASCII_String* Address)
;
;  Parameter:
;   dword            Number     = The Number
;   dw2ASCII_String* Address    = The destination string
;
;  Return value:
;   nothing
;
;  Description:
;   Converts the number stored in the dword into ASCII characters and stores
;   them in the string.
;   This programm does not affect any of the registers. No return value in AX,
;   AX is not changed.
;
;  Callingconvention:
;   You have to push the number you want to convert and then the address of
;   the destination string.
;   Example:
;
;      mov EAX, -345345
;      push EAX
;      push dw2ASCII_StringAdr
;      call dw2uhex
;

    dw2uhex:
    pushf
    pushad
        
        mov BP, SP
        sub SP, 22
        
        ; Cacheline
        ; 30         word[BP + 28h] = HW Number
        ; 28         word[BP + 26h] = LW Number
        ; 26         word[BP + 24h] = dw2ASCII_String* Address
        ; 24         word[BP + 22h] = IP
        ; 22         word[BP + 20h] = Safed environment FLAGS
        ; 20         word[BP + 1Eh] = Safed environment HW EAX
        ; 18         word[BP + 1Ch] = Safed environment LW EAX
        ; 16         word[BP + 1Ah] = Safed environment HW ECX
        ; 14         word[BP + 18h] = Safed environment LW ECX
        ; 12         word[BP + 16h] = Safed environment HW EDX
        ; 10         word[BP + 14h] = Safed environment LW EDX
        ; 08         word[BP + 12h] = Safed environment HW EBX
        ; 06         word[BP + 10h] = Safed environment LW EBX
        ; 04         word[BP + 0Eh] = Safed environment HW ESP
        ; 02         word[BP + 0Ch] = Safed environment LW ESP
        ; 00         word[BP + 0Ah] = Safed environment HW EBP
        ; 30         word[BP + 08h] = Safed environment LW EBP
        ; 28         word[BP + 06h] = Safed environment HW ESI
        ; 26         word[BP + 04h] = Safed environment LW ESI
        ; 24         word[BP + 02h] = Safed environment HW EDI
        ; 22 <BP     word[BP + 00h] = Safed environment LW EDI
        ; 20         
        ; 18         
        ; 16         
        ; 14         
        ; 12         
        ; 10         
        ; 08         
        ; 06         
        ; 04         
        ; 02         
        ; 00     <SP 
        
        
        mov EAX, dword[BP + 26h]                   ; Zu konvertierende Zahl
        mov BX, word[BP + 24h]                     ; String Adresse
        
        mov byte[BX + dw2ASCII_String.s], "+"
        mov byte[BX + dw2ASCII_String.x], "h"
        
        jmp Convert2Hex

;-------------------------------------------------------------------------------
;
;  dw2shex                                                         Size: n Bytes
;
;
;  Brief:
;   Converts the signed doubleword into a signed hexadecimal number and stores
;   it into a string.
;
;  Prototype:
;   nothing dw2shex (dword Number, dw2ASCII_String* Address)
;
;  Parameter:
;   dword            Number     = The Number
;   dw2ASCII_String* Address    = The destination string
;
;  Return value:
;   nothing
;
;  Description:
;   Convert's the number stored in the dword into ASCII characters and stores
;   them in the string.
;   This programm does not affect any of the registers. No return value in AX,
;   AX is not changed.
;
;  Callingconvention:
;   You have to push the number you want to convert and then the address of
;   the destination string.
;   Example:
;
;      mov EAX, -345345
;      push EAX
;      push dw2ASCII_StringAdr
;      call dw2shex
;

    dw2shex:
    pushf
    pushad
        
        mov BP, SP
        sub SP, 22
        
        ; Cacheline
        ; 30         word[BP + 28h] = HW Number
        ; 28         word[BP + 26h] = LW Number
        ; 26         word[BP + 24h] = dw2ASCII_String* Address
        ; 24         word[BP + 22h] = IP
        ; 22         word[BP + 20h] = Safed environment FLAGS
        ; 20         word[BP + 1Eh] = Safed environment HW EAX
        ; 18         word[BP + 1Ch] = Safed environment LW EAX
        ; 16         word[BP + 1Ah] = Safed environment HW ECX
        ; 14         word[BP + 18h] = Safed environment LW ECX
        ; 12         word[BP + 16h] = Safed environment HW EDX
        ; 10         word[BP + 14h] = Safed environment LW EDX
        ; 08         word[BP + 12h] = Safed environment HW EBX
        ; 06         word[BP + 10h] = Safed environment LW EBX
        ; 04         word[BP + 0Eh] = Safed environment HW ESP
        ; 02         word[BP + 0Ch] = Safed environment LW ESP
        ; 00         word[BP + 0Ah] = Safed environment HW EBP
        ; 30         word[BP + 08h] = Safed environment LW EBP
        ; 28         word[BP + 06h] = Safed environment HW ESI
        ; 26         word[BP + 04h] = Safed environment LW ESI
        ; 24         word[BP + 02h] = Safed environment HW EDI
        ; 22 <BP     word[BP + 00h] = Safed environment LW EDI
        ; 20         
        ; 18         
        ; 16         
        ; 14         
        ; 12         
        ; 10         
        ; 08         
        ; 06         
        ; 04         
        ; 02         
        ; 00     <SP 
        
        
        mov EAX, dword[BP + 26h]                   ; Zu konvertierende Zahl
        mov BX, word[BP + 24h]                     ; String Adresse
        
        mov byte[BX + dw2ASCII_String.s], "+"
        test EAX, 10000000000000000000000000000000b
        jns .NotSigned
            
            add byte[BX + dw2ASCII_String.s], "-" - "+"
            not EAX
            inc EAX
            
        .NotSigned:
        
        mov byte[BX + dw2ASCII_String.x], "h"
        
        jmp Convert2Hex

;-------------------------------------------------------------------------------
;
;  dw2udec                                                         Size: n Bytes
;
;
;  Brief:
;   Converts the unsigned doubleword into a unsigned decimal number and stores
;   it into a string.
;
;  Prototype:
;   nothing dw2udec (dword Number, dw2ASCII_String* Address)
;
;  Parameter:
;   dword            Number     = The Number
;   dw2ASCII_String* Address    = The destination string
;
;  Return value:
;   nothing
;
;  Description:
;   Converts the number stored in the dword into ASCII characters and stores
;   them in the string.
;   This programm does not affect any of the registers. No return value in AX,
;   AX is not changed.
;
;  Callingconvention:
;   You have to push the number you want to convert and then the address of
;   the destination string.
;   Example:
;
;      mov EAX, -345345
;      push EAX
;      push dw2ASCII_StringAdr
;      call dw2udec
;

    dw2udec:
    pushf
    pushad
        
        mov BP, SP
        sub SP, 22
        
        ; Cacheline
        ; 30         word[BP + 28h] = HW Number
        ; 28         word[BP + 26h] = LW Number
        ; 26         word[BP + 24h] = dw2ASCII_String* Address
        ; 24         word[BP + 22h] = IP
        ; 22         word[BP + 20h] = Safed environment FLAGS
        ; 20         word[BP + 1Eh] = Safed environment HW EAX
        ; 18         word[BP + 1Ch] = Safed environment LW EAX
        ; 16         word[BP + 1Ah] = Safed environment HW ECX
        ; 14         word[BP + 18h] = Safed environment LW ECX
        ; 12         word[BP + 16h] = Safed environment HW EDX
        ; 10         word[BP + 14h] = Safed environment LW EDX
        ; 08         word[BP + 12h] = Safed environment HW EBX
        ; 06         word[BP + 10h] = Safed environment LW EBX
        ; 04         word[BP + 0Eh] = Safed environment HW ESP
        ; 02         word[BP + 0Ch] = Safed environment LW ESP
        ; 00         word[BP + 0Ah] = Safed environment HW EBP
        ; 30         word[BP + 08h] = Safed environment LW EBP
        ; 28         word[BP + 06h] = Safed environment HW ESI
        ; 26         word[BP + 04h] = Safed environment LW ESI
        ; 24         word[BP + 02h] = Safed environment HW EDI
        ; 22 <BP     word[BP + 00h] = Safed environment LW EDI
        ; 20         
        ; 18         
        ; 16         
        ; 14         
        ; 12         
        ; 10         
        ; 08         
        ; 06         
        ; 04         
        ; 02         
        ; 00     <SP 
        
        
        mov EAX, dword[BP + 26h]                   ; Zu konvertierende Zahl
        mov BX, word[BP + 24h]                     ; String Adresse
        
        mov byte[BX + dw2ASCII_String.s], "+"
        mov byte[BX + dw2ASCII_String.x], "d"
        
        jmp Convert2Dec

;-------------------------------------------------------------------------------
;
;  dw2sdec                                                         Size: n Bytes
;
;
;  Brief:
;   Converts the signed doubleword into a signed decimal number and stores it 
;   into a string.
;
;  Prototype:
;   nothing dw2sdec (dword Number, dw2ASCII_String* Address)
;
;  Parameter:
;   dword            Number     = The Number
;   dw2ASCII_String* Address    = The destination string
;
;  Return value:
;   nothing
;
;  Description:
;   Converts the number stored in the dword into ASCII characters and stores
;   them in the string.
;   This programm does not affect any of the registers. No return value in AX,
;   AX is not changed.
;
;  Callingconvention:
;   You have to push the number you want to convert and then the address of
;   the destination string.
;   Example:
;
;      mov EAX, -345345
;      push EAX
;      push dw2ASCII_StringAdr
;      call dw2sdec
;

    dw2sdec:
    pushf
    pushad
        
        mov BP, SP
        sub SP, 22
        
        ; Cacheline
        ; 30         word[BP + 28h] = HW Number
        ; 28         word[BP + 26h] = LW Number
        ; 26         word[BP + 24h] = dw2ASCII_String* Address
        ; 24         word[BP + 22h] = IP
        ; 22         word[BP + 20h] = Safed environment FLAGS
        ; 20         word[BP + 1Eh] = Safed environment HW EAX
        ; 18         word[BP + 1Ch] = Safed environment LW EAX
        ; 16         word[BP + 1Ah] = Safed environment HW ECX
        ; 14         word[BP + 18h] = Safed environment LW ECX
        ; 12         word[BP + 16h] = Safed environment HW EDX
        ; 10         word[BP + 14h] = Safed environment LW EDX
        ; 08         word[BP + 12h] = Safed environment HW EBX
        ; 06         word[BP + 10h] = Safed environment LW EBX
        ; 04         word[BP + 0Eh] = Safed environment HW ESP
        ; 02         word[BP + 0Ch] = Safed environment LW ESP
        ; 00         word[BP + 0Ah] = Safed environment HW EBP
        ; 30         word[BP + 08h] = Safed environment LW EBP
        ; 28         word[BP + 06h] = Safed environment HW ESI
        ; 26         word[BP + 04h] = Safed environment LW ESI
        ; 24         word[BP + 02h] = Safed environment HW EDI
        ; 22 <BP     word[BP + 00h] = Safed environment LW EDI
        ; 20         
        ; 18         
        ; 16         
        ; 14         
        ; 12         
        ; 10         
        ; 08         
        ; 06         
        ; 04         
        ; 02         
        ; 00     <SP 
        
        
        mov EAX, dword[BP + 26h]                   ; Zu konvertierende Zahl
        mov BX, word[BP + 24h]                     ; String Adresse
        
        mov byte[BX + dw2ASCII_String.s], "+"
        test EAX, 10000000000000000000000000000000b
        jns .NotSigned
            
            add byte[BX + dw2ASCII_String.s], "-" - "+"
            not EAX
            inc EAX
            
        .NotSigned:
        
        mov byte[BX + dw2ASCII_String.x], "d"
        
        jmp Convert2Dec

;-------------------------------------------------------------------------------
;
;  dw2ubin                                                        Size: 44 Bytes
;
;
;  Brief:
;   Converts the unsigned doubleword into a unsigned binary number and stores it
;   into a string.
;
;  Prototype:
;   nothing dw2ubin (dword Number, dw2ASCII_String* Address)
;
;  Parameter:
;   dword            Number     = The Number
;   dw2ASCII_String* Address    = The destination string
;
;  Return value:
;   nothing
;
;  Description:
;   Converts the number stored in the dword into ASCII characters and stores
;   them in the string.
;   This programm does not affect any of the registers. No return value in AX,
;   AX is not changed.
;
;  Callingconvention:
;   You have to push the number you want to convert and then the address of
;   the destination string.
;   Example:
;
;      mov EAX, -345345
;      push EAX
;      push dw2ASCII_StringAdr
;      call dw2ubin
;

    dw2ubin:
    pushf
    pushad
        
        mov BP, SP
        sub SP, 22
        
        ; Cacheline
        ; 30         word[BP + 28h] = HW Number
        ; 28         word[BP + 26h] = LW Number
        ; 26         word[BP + 24h] = dw2ASCII_String* Address
        ; 24         word[BP + 22h] = IP
        ; 22         word[BP + 20h] = Safed environment FLAGS
        ; 20         word[BP + 1Eh] = Safed environment HW EAX
        ; 18         word[BP + 1Ch] = Safed environment LW EAX
        ; 16         word[BP + 1Ah] = Safed environment HW ECX
        ; 14         word[BP + 18h] = Safed environment LW ECX
        ; 12         word[BP + 16h] = Safed environment HW EDX
        ; 10         word[BP + 14h] = Safed environment LW EDX
        ; 08         word[BP + 12h] = Safed environment HW EBX
        ; 06         word[BP + 10h] = Safed environment LW EBX
        ; 04         word[BP + 0Eh] = Safed environment HW ESP
        ; 02         word[BP + 0Ch] = Safed environment LW ESP
        ; 00         word[BP + 0Ah] = Safed environment HW EBP
        ; 30         word[BP + 08h] = Safed environment LW EBP
        ; 28         word[BP + 06h] = Safed environment HW ESI
        ; 26         word[BP + 04h] = Safed environment LW ESI
        ; 24         word[BP + 02h] = Safed environment HW EDI
        ; 22 <BP     word[BP + 00h] = Safed environment LW EDI
        ; 20         
        ; 18         
        ; 16         
        ; 14         
        ; 12         
        ; 10         
        ; 08         
        ; 06         
        ; 04         
        ; 02         
        ; 00     <SP 
        
        
        mov EAX, dword[BP + 26h]                   ; Zu konvertierende Zahl
        mov BX, word[BP + 24h]                     ; String Adresse
        
        mov byte[BX + dw2ASCII_String.s], "+"
        mov byte[BX + dw2ASCII_String.x], "b"
        
        jmp Convert2Bin

;-------------------------------------------------------------------------------
;
;  dw2sbin                                                         Size: n Bytes
;
;
;  Brief:
;   Converts the signed doubleword into a signed binary number and stores it
;   into a string.
;
;  Prototype:
;   nothing dw2sbin (dword Number, dw2ASCII_String* Address)
;
;  Parameter:
;   dword            Number     = The Number
;   dw2ASCII_String* Address    = The destination string
;
;  Return value:
;   nothing
;
;  Description:
;   Converts the number stored in the dword into ASCII characters and stores
;   them in the string.
;   This programm does not affect any of the registers. No return value in AX,
;   AX is not changed.
;
;  Callingconvention:
;   You have to push the number you want to convert and then the address of
;   the destination string.
;   Example:
;
;      mov EAX, -345345
;      push EAX
;      push dw2ASCII_StringAdr
;      call dw2sbin
;

    dw2sbin:
    pushf
    pushad
        
        mov BP, SP
        sub SP, 22
        
        ; Cacheline
        ; 30         word[BP + 28h] = HW Number
        ; 28         word[BP + 26h] = LW Number
        ; 26         word[BP + 24h] = dw2ASCII_String* Address
        ; 24         word[BP + 22h] = IP
        ; 22         word[BP + 20h] = Safed environment FLAGS
        ; 20         word[BP + 1Eh] = Safed environment HW EAX
        ; 18         word[BP + 1Ch] = Safed environment LW EAX
        ; 16         word[BP + 1Ah] = Safed environment HW ECX
        ; 14         word[BP + 18h] = Safed environment LW ECX
        ; 12         word[BP + 16h] = Safed environment HW EDX
        ; 10         word[BP + 14h] = Safed environment LW EDX
        ; 08         word[BP + 12h] = Safed environment HW EBX
        ; 06         word[BP + 10h] = Safed environment LW EBX
        ; 04         word[BP + 0Eh] = Safed environment HW ESP
        ; 02         word[BP + 0Ch] = Safed environment LW ESP
        ; 00         word[BP + 0Ah] = Safed environment HW EBP
        ; 30         word[BP + 08h] = Safed environment LW EBP
        ; 28         word[BP + 06h] = Safed environment HW ESI
        ; 26         word[BP + 04h] = Safed environment LW ESI
        ; 24         word[BP + 02h] = Safed environment HW EDI
        ; 22 <BP     word[BP + 00h] = Safed environment LW EDI
        ; 20         
        ; 18         
        ; 16         
        ; 14         
        ; 12         
        ; 10         
        ; 08         
        ; 06         
        ; 04         
        ; 02         
        ; 00     <SP 
        
        
        mov EAX, dword[BP + 26h]                   ; Zu konvertierende Zahl
        mov BX, word[BP + 24h]                     ; String Adresse
        
        mov byte[BX + dw2ASCII_String.s], "+"
        test EAX, 10000000000000000000000000000000b
        jns .NotSigned
            
            add byte[BX + dw2ASCII_String.s], "-" - "+"
            not EAX
            inc EAX
            
        .NotSigned:
        
        mov byte[BX + dw2ASCII_String.x], "b"
        
        jmp Convert2Bin
    
    Convert2Hex:
        
        .Initialize:
            
            mov ECX, 16                                ; Divisor
            mov DI, dw2ASCII_String.n + MaxDigitSize   ; Index into String
        
        .Convert:
            
            mov EDX, 0
            dec DI
            div ECX
            mov byte[BX+DI], DL
            cmp EAX, 0
            jne .Convert
        
        cmp DI, dw2ASCII_String.n
        je .End
        
        .FillWithZero:
            
            dec DI
            mov byte[BX+DI], 0
            cmp DI, dw2ASCII_String.n
            jne .FillWithZero
        
        .End:
            
            jmp Convert2ASCII
    
    Convert2Dec:
        
        .Initialize:
            
            mov ECX, 10                                ; Divisor
            mov DI, dw2ASCII_String.n + MaxDigitSize   ; Index into String
        
        .Convert:
            
            mov EDX, 0
            dec DI
            div ECX
            mov byte[BX+DI], DL
            cmp EAX, 0
            jne .Convert
        
        cmp DI, dw2ASCII_String.n
        je .End
        
        .FillWithZero:
            
            dec DI
            mov byte[BX+DI], 0
            cmp DI, dw2ASCII_String.n
            jne .FillWithZero
        
        .End:
            
            jmp Convert2ASCII
    
    Convert2Bin:
        
        .Initialize:
            
            mov ECX, 2                                 ; Divisor
            mov DI, dw2ASCII_String.n + MaxDigitSize   ; Index into String
        
        .Convert:
            
            mov EDX, 0
            dec DI
            div ECX
            mov byte[BX+DI], DL
            cmp EAX, 0
            jne .Convert
        
        cmp DI, dw2ASCII_String.n
        je .End
        
        .FillWithZero:
            
            dec DI
            mov byte[BX+DI], 0
            cmp DI, dw2ASCII_String.n
            jne .FillWithZero
        
        .End:
            
            jmp Convert2ASCII
    
    Convert2ASCII:
        
        .Initialize:
            
            mov DI, dw2ASCII_String.n + MaxDigitSize   ; Index into String
        
        .Convert:
        dec DI
            
            movzx SI, byte[BX+DI]
            mov AL, byte[dw2nnn_LUT + SI]
            mov byte[BX+DI], AL
            
        cmp DI, dw2ASCII_String.n
        jne .Convert
        
        .End:
            
            add SP, 22
            
        
    popad
    popf
    ret 6
    
    
    
    
;== data =======================================================================
[segment .data]
    
    align 16
    dw2nnn_LUT:
    .char_0:    db "0"
    .char_1:    db "1"
    .char_2:    db "2"
    .char_3:    db "3"
    .char_4:    db "4"
    .char_5:    db "5"
    .char_6:    db "6"
    .char_7:    db "7"
    .char_8:    db "8"
    .char_9:    db "9"
    .char_A:    db "A"
    .char_B:    db "B"
    .char_C:    db "C"
    .char_D:    db "D"
    .char_E:    db "E"
    .char_F:    db "F"
    
%endif

Alex
Posts: 16
Joined: Sun Jan 18, 2009 2:12 am

Re: Code i want to share

Post by Alex »

Hi :)

Later on i will post some code with a "divide error handler" witch uses the "print" and the "dw2ASCII" code pieces to display some information were the error occurred.
It will also have a routine to "mount" the handler :)

Alex
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Code i want to share

Post by gravaera »

Hey thank bro, I need this codes.

I having trouble to get my kernel to print strings I ask it to. Say, you can translate this codes to C too? So other who use diferent code language for kernel can use? [-o< [-o< My kernel is C/C++ :oops: And how I use this codes? NASM or FASM or GAS? Which one work faster?

Good job btw =D>
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
Alex
Posts: 16
Joined: Sun Jan 18, 2009 2:12 am

Re: Code i want to share

Post by Alex »

@gravaera:
Thank you :)
These code snippets were written for my little bootloader. You have to use NASM to assamble them as mentioned above :)
As you are writing in C i assume you are already in protectet mode. In protectet mode you can't use the BIOS, but my programs "print", "FDD_Resst" and "FDD_Read_Sector" do use the BIOS, so it will not work in your case.
If you are coding for real mode you can use my progs with a few changes :)

Im not as god in coding C as i am in coding asm. I could make the changes for you, but my current goal is to write the "divide error interrupt handler" :)
If i would do all the work people are asking me to do, i would not have time for my projects ;) i hope you anderstand this :)
If i can help you in litle time, no probelm :) but this would cost a lot of time, because i would have to study my self, how to convert it :)

Alex
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Code i want to share

Post by JamesM »

Alex wrote:@gravaera:
Thank you :)
These code snippets were written for my little bootloader. You have to use NASM to assamble them as mentioned above :)
As you are writing in C i assume you are already in protectet mode. In protectet mode you can't use the BIOS, but my programs "print", "FDD_Resst" and "FDD_Read_Sector" do use the BIOS, so it will not work in your case.
If you are coding for real mode you can use my progs with a few changes :)

Im not as god in coding C as i am in coding asm. I could make the changes for you, but my current goal is to write the "divide error interrupt handler" :)
If i would do all the work people are asking me to do, i would not have time for my projects ;) i hope you anderstand this :)
If i can help you in litle time, no probelm :) but this would cost a lot of time, because i would have to study my self, how to convert it :)

Alex
He's trolling you.
Alex
Posts: 16
Joined: Sun Jan 18, 2009 2:12 am

Re: Code i want to share

Post by Alex »

O hahaha :D
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Code i want to share

Post by Gigasoft »

Later on i will post some code with a "divide error handler" witch uses the "print" and the "dw2ASCII" code pieces to display some information were the error occurred.
I think I've seen enough for a final verdict.

Grade: D-
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Code i want to share

Post by gravaera »

http://cdn.memegenerator.net/instances/ ... 171266.jpg

Back in my heyday I was quite the prankster... xD
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
Alex
Posts: 16
Joined: Sun Jan 18, 2009 2:12 am

Re: Code i want to share

Post by Alex »

I had god intentions and wanted to help other people who are interestet in OS programing. But i see this is not a god idea.
Il stop to post code and make my own thing.
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Code i want to share

Post by shikhin »

Alex wrote:I had god intentions and wanted to help other people who are interestet in OS programing. But i see this is not a god idea.
Il stop to post code and make my own thing.
Alex wrote: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.
Alex wrote:This programm does not affect any of the registers.
Alex wrote:Our lokal variables and some other things will be put on this.
Forum Rules wrote:Please try to write legibly. (...) Use a spell checker.
http://shikhin.in/

Current status: Gandr.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Code i want to share

Post by JamesM »

Shikhin wrote:
Alex wrote:I had god intentions and wanted to help other people who are interestet in OS programing. But i see this is not a god idea.
Il stop to post code and make my own thing.
Alex wrote: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.
Alex wrote:This programm does not affect any of the registers.
Alex wrote:Our lokal variables and some other things will be put on this.
Forum Rules wrote:Please try to write legibly. (...) Use a spell checker.
That's a bit harsh Shinkin. The spelling errors cause no difficulty in comprehension and some would not have been caught by a spell checker ("god" vs "good" for example).
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Code i want to share

Post by shikhin »

Hey,
JamesM wrote:That's a bit harsh Shinkin.
:lol:

OTOH, I should have perhaps added, "Not trying to be rude, but you should try to: <forum rules quote>". Apologies if it sounded too harsh. :)

Regards,
Shikhin
http://shikhin.in/

Current status: Gandr.
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: Code i want to share

Post by Rudster816 »

You should probably specify a license for the source code. Right now, people technically aren't allowed to use it.

http://wiki.osdev.org/Licensing
Locked