Help needed with 32 bit Checksum program - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help needed with 32 bit Checksum program

Luke7Luke7 Posts: 156Member
I would like to do two things with this source.
a. Convert it to an .exe than can be compiled by either Masm or Tasm
since buf db 0 is hardcoded, I haven't figured out how to make
it work
b. Incorporate the checksum into it
My attempts at that have not worked

Thanks.

; chkfile.asm - Modifications to do a CRC on a specified file
;
; Performs a CRC crc1.com
; Com file
;
; Original code by James Vahn, based on Edwin T. Floyd 's CRC-32 routine.
; May 15, 1994 Modifications by Andrew Kennedy
;
; Furthur improvements:
;
; encrypt filename and crc values
;

cseg segment
assume cs:cseg,ds:cseg
org 100h ;COM format

Begin proc far

mov ax,03D00h ;Open for read.
mov dx,offset filename
int 21h
jc error2
mov word ptr [hndl],ax
read:
mov ah,03Fh
mov bx,word ptr [hndl]
mov dx,offset buf
mov cx,0FEFFh ;Leave room in buffer for stack.
sub cx,dx ;Maximum buffer size.
int 21h
jc error3
or ax,ax ;End of file?
je exit
call UpdateCRC32 ;calculate CRC.
loop read
exit:
xor word ptr [initcrc],0FFFFh ;Finish CRC calculation.
xor word ptr [initcrc+2],0FFFFh
mov ax,word ptr [initcrc+2]
call bin2hex ;Print CRC to screen.
mov ax,word ptr [initcrc]
call bin2he
mov ax,0FFFFh
mov word ptr[initcrc],ax
mov word ptr[initcrc+2],ax
mov dx,offset CRLF ;linefeed
mov ah,09h
int 21h
close:
mov bx,word ptr [hndl]
mov ah,3Eh ;close file.
int 21h

mov ax,04C00h ;Normal exit to DOS.
int 21h

Begin endp
;*****************************************************

error proc near

mov ah,09h
mov dx,offset errmsg
int 21h
mov ax,04C01h ;Exit errorlevel 1.
int 21h
error endp
;*****************************************************

error2 proc near

mov ah,09h
mov dx,offset errmsg2
int 21h
mov ax,04C02h ;Exit errorlevel 2.
int 21h
error2 endp
;*****************************************************

error3 proc near

mov ah,09h
mov dx,offset errmsg3
int 21h
mov ax,04C03h ;Exit errorlevel 3.
int 21h
error3 endp
;*****************************************************

bin2hex proc near

;Convert AX to HEX ASCII output

bin2he: mov cx,4 ;4 hex digits
mov bx,10h ;divisor
bin2h1: xor dx,dx ;zero DX for 16 bit divide
div bx ;leaves quotient in AX
add dl,'0' ;convert remainder to ASCII
cmp dl,'9'
jna bin2h2
add dl,'A'-'9'-1
bin2h2: push dx ;put on stack
loop bin2h1 ;repeat
mov cx,4
bin2h3: pop ax ;pop most significant digit first
mov dl,al
mov ah,02h
int 21h
loop bin2h3
ret
bin2hex endp
;*****************************************************

UpdateCRC32 proc near

; UpdateCRC32 takes an initial CRC value and updates it from
; data in 'buf'. The updated CRC is then stored in InitCRC.

mov si,offset buf
mov cx,ax
jcxz bye
les ax,[initcrc]
mov dx,ES
lo2:
xor bh,bh
mov bl,al
lodsb
xor bl,al
mov al,ah
mov ah,dl
mov dl,dh
xor dh,dh
shl bx,1
shl bx,1
les bx,[crc32tab+bx]
xor ax,bx
mov bx,ES
xor dx,bx
loop lo2
mov word ptr [initcrc],ax ; Store result in InitCRC
mov word ptr [initcrc+2],dx
bye:
ret
UpdateCRC32 endp

;*****************************************************
; 32 bit reverse crc table for polynomial EDB88320h (Zmodem,PKZip)
; X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0

crc32tab dd 000000000h, 077073096h, 0ee0e612ch, 0990951bah, 0076dc419h
dd 0706af48fh, 0e963a535h, 09e6495a3h, 00edb8832h, 079dcb8a4h
dd 0e0d5e91eh, 097d2d988h, 009b64c2bh, 07eb17cbdh, 0e7b82d07h
dd 090bf1d91h, 01db71064h, 06ab020f2h, 0f3b97148h, 084be41deh
dd 01adad47dh, 06ddde4ebh, 0f4d4b551h, 083d385c7h, 0136c9856h
dd 0646ba8c0h, 0fd62f97ah, 08a65c9ech, 014015c4fh, 063066cd9h
dd 0fa0f3d63h, 08d080df5h, 03b6e20c8h, 04c69105eh, 0d56041e4h
dd 0a2677172h, 03c03e4d1h, 04b04d447h, 0d20d85fdh, 0a50ab56bh
dd 035b5a8fah, 042b2986ch, 0dbbbc9d6h, 0acbcf940h, 032d86ce3h
dd 045df5c75h, 0dcd60dcfh, 0abd13d59h, 026d930ach, 051de003ah
dd 0c8d75180h, 0bfd06116h, 021b4f4b5h, 056b3c423h, 0cfba9599h
dd 0b8bda50fh, 02802b89eh, 05f058808h, 0c60cd9b2h, 0b10be924h
dd 02f6f7c87h, 058684c11h, 0c1611dabh, 0b6662d3dh, 076dc4190h
dd 001db7106h, 098d220bch, 0efd5102ah, 071b18589h, 006b6b51fh
dd 09fbfe4a5h, 0e8b8d433h, 07807c9a2h, 00f00f934h, 09609a88eh
dd 0e10e9818h, 07f6a0dbbh, 0086d3d2dh, 091646c97h, 0e6635c01h
dd 06b6b51f4h, 01c6c6162h, 0856530d8h, 0f262004eh, 06c0695edh
dd 01b01a57bh, 08208f4c1h, 0f50fc457h, 065b0d9c6h, 012b7e950h
dd 08bbeb8eah, 0fcb9887ch, 062dd1ddfh, 015da2d49h, 08cd37cf3h
dd 0fbd44c65h, 04db26158h, 03ab551ceh, 0a3bc0074h, 0d4bb30e2h
dd 04adfa541h, 03dd895d7h, 0a4d1c46dh, 0d3d6f4fbh, 04369e96ah
dd 0346ed9fch, 0ad678846h, 0da60b8d0h, 044042d73h, 033031de5h
dd 0aa0a4c5fh, 0dd0d7cc9h, 05005713ch, 0270241aah, 0be0b1010h
dd 0c90c2086h, 05768b525h, 0206f85b3h, 0b966d409h, 0ce61e49fh
dd 05edef90eh, 029d9c998h, 0b0d09822h, 0c7d7a8b4h, 059b33d17h
dd 02eb40d81h, 0b7bd5c3bh, 0c0ba6cadh, 0edb88320h, 09abfb3b6h
dd 003b6e20ch, 074b1d29ah, 0ead54739h, 09dd277afh, 004db2615h
dd 073dc1683h, 0e3630b12h, 094643b84h, 00d6d6a3eh, 07a6a5aa8h
dd 0e40ecf0bh, 09309ff9dh, 00a00ae27h, 07d079eb1h, 0f00f9344h
dd 08708a3d2h, 01e01f268h, 06906c2feh, 0f762575dh, 0806567cbh
dd 0196c3671h, 06e6b06e7h, 0fed41b76h, 089d32be0h, 010da7a5ah
dd 067dd4acch, 0f9b9df6fh, 08ebeeff9h, 017b7be43h, 060b08ed5h
dd 0d6d6a3e8h, 0a1d1937eh, 038d8c2c4h, 04fdff252h, 0d1bb67f1h
dd 0a6bc5767h, 03fb506ddh, 048b2364bh, 0d80d2bdah, 0af0a1b4ch
dd 036034af6h, 041047a60h, 0df60efc3h, 0a867df55h, 0316e8eefh
dd 04669be79h, 0cb61b38ch, 0bc66831ah, 0256fd2a0h, 05268e236h
dd 0cc0c7795h, 0bb0b4703h, 0220216b9h, 05505262fh, 0c5ba3bbeh
dd 0b2bd0b28h, 02bb45a92h, 05cb36a04h, 0c2d7ffa7h, 0b5d0cf31h
dd 02cd99e8bh, 05bdeae1dh, 09b64c2b0h, 0ec63f226h, 0756aa39ch
dd 0026d930ah, 09c0906a9h, 0eb0e363fh, 072076785h, 005005713h
dd 095bf4a82h, 0e2b87a14h, 07bb12baeh, 00cb61b38h, 092d28e9bh
dd 0e5d5be0dh, 07cdcefb7h, 00bdbdf21h, 086d3d2d4h, 0f1d4e242h
dd 068ddb3f8h, 01fda836eh, 081be16cdh, 0f6b9265bh, 06fb077e1h
dd 018b74777h, 088085ae6h, 0ff0f6a70h, 066063bcah, 011010b5ch
dd 08f659effh, 0f862ae69h, 0616bffd3h, 0166ccf45h, 0a00ae278h
dd 0d70dd2eeh, 04e048354h, 03903b3c2h, 0a7672661h, 0d06016f7h
dd 04969474dh, 03e6e77dbh, 0aed16a4ah, 0d9d65adch, 040df0b66h
dd 037d83bf0h, 0a9bcae53h, 0debb9ec5h, 047b2cf7fh, 030b5ffe9h
dd 0bdbdf21ch, 0cabac28ah, 053b39330h, 024b4a3a6h, 0bad03605h
dd 0cdd70693h, 054de5729h, 023d967bfh, 0b3667a2eh, 0c4614ab8h
dd 05d681b02h, 02a6f2b94h, 0b40bbe37h, 0c30c8ea1h, 05a05df1bh
dd 02d02ef8dh

initcrc dd 0FFFFFFFFh ;Initial CRC value

errmsg db 'File not found- Useage: CRC FILENAME.*',0Dh,0Ah,24h
errmsg2 db 'error 2, cannot open file.',0Dh,0Ah,24h
errmsg3 db 'error 3, cannot read file.',0Dh,0Ah,24h
crlf db 0Dh,0Ah,24h
dirmsg db 9,'
    ',0Dh,0Ah,24h

    hndl dw 0 ; File handle storage.
    buf db 0 ; Dynamic storage. Do not move!
    filename db 'crc1.com',0 ; filename to do a CRC check on

    cseg ends
    end Begin


Comments

  • jeffleydajeffleyda Posts: 390Member
    : I would like to do two things with this source.
    : a. Convert it to an .exe than can be compiled by either Masm or Tasm
    : since buf db 0 is hardcoded, I haven't figured out how to make
    : it work
    : b. Incorporate the checksum into it
    : My attempts at that have not worked
    :

    I'm not sure what you mean in 'b', but here's a version of the program that is compilable with masm or tasm, compiles to an .exe, and uses the command line to supply the filename, not hardcoded.

    The 'buf db 0' being hardcoded in the program doesn't have any effect on DOS programs since all extra memory is allocated to programs when running under DOS, it won't be overwriting anything important.
    Just don't add any variables after the 'buf db 0' and you should be ok.

    -jeff!

    [code]
    CODE SEGMENT
    assume cs:code

    ; MAIN CODE STARTING POINT

    Start:

    mov ah, 51h ; get PSP address
    int 21h
    mov ds, bx


    mov bx, 80h ; args always start at 80h
    xor ch, ch
    mov cl, byte ptr [bx] ; get length of PSP
    add bx, cx
    inc bx
    mov byte ptr ds:[bx], 0 ; terminate with a space

    mov dx, 82h ; point to command line
    mov ax, 03D00h ; Open for read.
    int 21h

    push cs
    pop ds

    jc error2
    mov word ptr cs:[hndl], ax
    read:
    mov ah, 03Fh
    mov bx, word ptr cs:[hndl]
    mov dx, offset buf
    mov cx, 0FEFFh ; Leave room in buffer for stack.
    sub cx, dx ; Maximum buffer size.
    int 21h
    jc error3
    or ax, ax ; End of file?
    je fini
    call UpdateCRC32 ; calculate CRC.
    loop read
    fini:
    xor word ptr cs:[initcrc], 0FFFFh ; Finish CRC calculation.
    xor word ptr cs:[initcrc+2], 0FFFFh
    mov ax, word ptr cs:[initcrc+2]
    call bin2hex ; Print CRC to screen.
    mov ax, word ptr cs:[initcrc]
    call bin2hex
    mov ax, 0FFFFh
    mov word ptr cs:[initcrc], ax
    mov word ptr cs:[initcrc+2], ax
    mov dx, offset CRLF ; linefeed
    mov ah, 09h
    int 21h
    close:
    mov bx, word ptr cs:[hndl]
    mov ah, 3Eh ; close file.
    int 21h
    mov al, 0 ; errorlevel #
    exit:
    mov ah, 04Ch ; exit with errorlevel
    int 21h

    ;*****************************************************
    error:
    mov ah, 09h
    mov dx, offset errmsg
    int 21h
    mov al, 01h ; Exit errorlevel 1.
    jmp exit

    ;*****************************************************
    error2:

    mov ah, 09h
    mov dx, offset errmsg2
    int 21h
    mov al, 2h ; Exit errorlevel 2.
    jmp exit

    ;*****************************************************
    error3:

    mov ah, 09h
    mov dx, offset errmsg3
    int 21h
    mov al, 3h ; Exit errorlevel 3.
    jmp exit

    ;*****************************************************
    bin2hex proc near
    ; Convert AX to HEX ASCII output

    bin2he:
    mov cx, 4 ; 4 hex digits
    mov bx, 10h ; divisor
    bin2h1:
    xor dx, dx ; zero DX for 16 bit divide
    div bx ; leaves quotient in AX
    add dl, '0' ; convert remainder to ASCII
    cmp dl, '9'
    jna bin2h2
    add dl, 'A'-'9'-1
    bin2h2:
    push dx ; put on stack
    loop bin2h1 ; repeat
    mov cx, 4
    bin2h3:
    pop ax ; pop most significant digit first
    mov dl, al
    mov ah, 02h
    int 21h
    loop bin2h3
    ret
    bin2hex endp

    ;*****************************************************
    UpdateCRC32 proc near

    ; UpdateCRC32 takes an initial CRC value and updates it from
    ; data in 'buf'. The updated CRC is then stored in InitCRC.

    mov si, offset buf
    mov cx, ax
    jcxz bye
    les ax, cs:[initcrc]
    mov dx, ES
    lo2:
    xor bh, bh
    mov bl, al
    lodsb
    xor bl, al
    mov al, ah
    mov ah, dl
    mov dl, dh
    xor dh, dh
    shl bx, 1
    shl bx, 1
    les bx, cs:[crc32tab+bx]
    xor ax, bx
    mov bx, ES
    xor dx, bx
    loop lo2
    mov word ptr cs:[initcrc], ax ; Store result in InitCRC
    mov word ptr cs:[initcrc+2], dx
    bye:
    ret
    UpdateCRC32 endp



    ;*****************************************************
    ; 32 bit reverse crc table for polynomial EDB88320h (Zmodem,PKZip)
    ; X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0

    crc32tab dd 000000000h, 077073096h, 0ee0e612ch, 0990951bah, 0076dc419h
    dd 0706af48fh, 0e963a535h, 09e6495a3h, 00edb8832h, 079dcb8a4h
    dd 0e0d5e91eh, 097d2d988h, 009b64c2bh, 07eb17cbdh, 0e7b82d07h
    dd 090bf1d91h, 01db71064h, 06ab020f2h, 0f3b97148h, 084be41deh
    dd 01adad47dh, 06ddde4ebh, 0f4d4b551h, 083d385c7h, 0136c9856h
    dd 0646ba8c0h, 0fd62f97ah, 08a65c9ech, 014015c4fh, 063066cd9h
    dd 0fa0f3d63h, 08d080df5h, 03b6e20c8h, 04c69105eh, 0d56041e4h
    dd 0a2677172h, 03c03e4d1h, 04b04d447h, 0d20d85fdh, 0a50ab56bh
    dd 035b5a8fah, 042b2986ch, 0dbbbc9d6h, 0acbcf940h, 032d86ce3h
    dd 045df5c75h, 0dcd60dcfh, 0abd13d59h, 026d930ach, 051de003ah
    dd 0c8d75180h, 0bfd06116h, 021b4f4b5h, 056b3c423h, 0cfba9599h
    dd 0b8bda50fh, 02802b89eh, 05f058808h, 0c60cd9b2h, 0b10be924h
    dd 02f6f7c87h, 058684c11h, 0c1611dabh, 0b6662d3dh, 076dc4190h
    dd 001db7106h, 098d220bch, 0efd5102ah, 071b18589h, 006b6b51fh
    dd 09fbfe4a5h, 0e8b8d433h, 07807c9a2h, 00f00f934h, 09609a88eh
    dd 0e10e9818h, 07f6a0dbbh, 0086d3d2dh, 091646c97h, 0e6635c01h
    dd 06b6b51f4h, 01c6c6162h, 0856530d8h, 0f262004eh, 06c0695edh
    dd 01b01a57bh, 08208f4c1h, 0f50fc457h, 065b0d9c6h, 012b7e950h
    dd 08bbeb8eah, 0fcb9887ch, 062dd1ddfh, 015da2d49h, 08cd37cf3h
    dd 0fbd44c65h, 04db26158h, 03ab551ceh, 0a3bc0074h, 0d4bb30e2h
    dd 04adfa541h, 03dd895d7h, 0a4d1c46dh, 0d3d6f4fbh, 04369e96ah
    dd 0346ed9fch, 0ad678846h, 0da60b8d0h, 044042d73h, 033031de5h
    dd 0aa0a4c5fh, 0dd0d7cc9h, 05005713ch, 0270241aah, 0be0b1010h
    dd 0c90c2086h, 05768b525h, 0206f85b3h, 0b966d409h, 0ce61e49fh
    dd 05edef90eh, 029d9c998h, 0b0d09822h, 0c7d7a8b4h, 059b33d17h
    dd 02eb40d81h, 0b7bd5c3bh, 0c0ba6cadh, 0edb88320h, 09abfb3b6h
    dd 003b6e20ch, 074b1d29ah, 0ead54739h, 09dd277afh, 004db2615h
    dd 073dc1683h, 0e3630b12h, 094643b84h, 00d6d6a3eh, 07a6a5aa8h
    dd 0e40ecf0bh, 09309ff9dh, 00a00ae27h, 07d079eb1h, 0f00f9344h
    dd 08708a3d2h, 01e01f268h, 06906c2feh, 0f762575dh, 0806567cbh
    dd 0196c3671h, 06e6b06e7h, 0fed41b76h, 089d32be0h, 010da7a5ah
    dd 067dd4acch, 0f9b9df6fh, 08ebeeff9h, 017b7be43h, 060b08ed5h
    dd 0d6d6a3e8h, 0a1d1937eh, 038d8c2c4h, 04fdff252h, 0d1bb67f1h
    dd 0a6bc5767h, 03fb506ddh, 048b2364bh, 0d80d2bdah, 0af0a1b4ch
    dd 036034af6h, 041047a60h, 0df60efc3h, 0a867df55h, 0316e8eefh
    dd 04669be79h, 0cb61b38ch, 0bc66831ah, 0256fd2a0h, 05268e236h
    dd 0cc0c7795h, 0bb0b4703h, 0220216b9h, 05505262fh, 0c5ba3bbeh
    dd 0b2bd0b28h, 02bb45a92h, 05cb36a04h, 0c2d7ffa7h, 0b5d0cf31h
    dd 02cd99e8bh, 05bdeae1dh, 09b64c2b0h, 0ec63f226h, 0756aa39ch
    dd 0026d930ah, 09c0906a9h, 0eb0e363fh, 072076785h, 005005713h
    dd 095bf4a82h, 0e2b87a14h, 07bb12baeh, 00cb61b38h, 092d28e9bh
    dd 0e5d5be0dh, 07cdcefb7h, 00bdbdf21h, 086d3d2d4h, 0f1d4e242h
    dd 068ddb3f8h, 01fda836eh, 081be16cdh, 0f6b9265bh, 06fb077e1h
    dd 018b74777h, 088085ae6h, 0ff0f6a70h, 066063bcah, 011010b5ch
    dd 08f659effh, 0f862ae69h, 0616bffd3h, 0166ccf45h, 0a00ae278h
    dd 0d70dd2eeh, 04e048354h, 03903b3c2h, 0a7672661h, 0d06016f7h
    dd 04969474dh, 03e6e77dbh, 0aed16a4ah, 0d9d65adch, 040df0b66h
    dd 037d83bf0h, 0a9bcae53h, 0debb9ec5h, 047b2cf7fh, 030b5ffe9h
    dd 0bdbdf21ch, 0cabac28ah, 053b39330h, 024b4a3a6h, 0bad03605h
    dd 0cdd70693h, 054de5729h, 023d967bfh, 0b3667a2eh, 0c4614ab8h
    dd 05d681b02h, 02a6f2b94h, 0b40bbe37h, 0c30c8ea1h, 05a05df1bh
    dd 02d02ef8dh

    initcrc dd 0FFFFFFFFh ;Initial CRC value

    errmsg db 'File not found- Useage: CRC FILENAME.*',0Dh,0Ah,24h
    errmsg2 db 'error 2, cannot open file.',0Dh,0Ah,24h
    errmsg3 db 'error 3, cannot read file.',0Dh,0Ah,24h
    crlf db 0Dh,0Ah,24h
    dirmsg db 9,'
      ',0Dh,0Ah,24h

      hndl dw 0 ; File handle storage.
      buf db 0 ; Dynamic storage. Do not move!



      CODE ENDS
      End Start

      [/code]

  • peretperet Posts: 69Member
    : The 'buf db 0' being hardcoded in the program doesn't have any effect on DOS programs since all extra memory is allocated to programs when running under DOS, it won't be overwriting anything important.

    That's not entirely true! Only COM files have that "privilege", and it's dangerous even so. Running out beyond the end of an EXE file will overwrite the DOS memory pointer chain and cause a crash when the program exits. The EXE header may SAY the program should be allocated all memory, but DOS only gives it the actual size.

    Besides, in the example program the buffer is bounded between the end of the code and the bottom of the stack, and the whole thing fits in one 64k segment. It could be done in an EXE by defining buf as the rest of the code segment ...
    [code]
    buf ds (0FFFFh-$) dup(?)
    [/code]
    ... and using CS overrides everywhere.

    Alternatively, do a MALLOC operation for buf space.
    [code]
    mov ax,4800h ; call MALLOC for file space
    mov bx,1000h ; request 64k
    ; BX has the required size in units of 16 bytes
    ; 64k is enough for this operation, though you can have more
    ; Segment address of allocated memory is returned in AX
    ; Carry set if operation fails
    int 21h
    mov [buf_seg],ax ; save segment address
    [/code]
    Then when you read the file, pass it [buf_seg] for the segment to load into.

    You're supposed to release memory before doing this, by using another Int 21 (I forget which) and giving it the last address of your program, otherwise MALLOC will fail. This is a nuisance, but the easy way round it is to link with the option /CP:1 - this makes the EXE header specify the true memory requirement instead of the default FFFF.

  • Luke7Luke7 Posts: 156Member
    : : I would like to do two things with this source.
    : : a. Convert it to an .exe than can be compiled by either Masm or Tasm
    : : since buf db 0 is hardcoded, I haven't figured out how to make
    : : it work
    : : b. Incorporate the checksum into it
    : : My attempts at that have not worked
    : :
    :
    : I'm not sure what you mean in 'b', but here's a version of the program that is compilable with masm or tasm, compiles to an .exe, and uses the command line to supply the filename, not hardcoded.

    Thanks for the .exe version. The second thing I needed was version that would store the checksum in it, do a checksum of itself and compare that
    against the stored value.

    I tried it in the com version but the value that's hard coded in doesn't match the checksum value the program calculates.

    I think I need to checksum up the where the value is stored but don't know how to do that.

    Thanks.
  • peretperet Posts: 69Member
    : I think I need to checksum up the where the value is stored but don't know how to do that.

    Traditionally, the CRC is tacked on to the end of the file. So if the file you're checking already has a CRC, you work up to 4 bytes from the end, then compare your result to the last four bytes.

    If you want to add the calculated CRC to the end of a file that doesn't have one, then you need to make some changes.

    First you need to open the file in read/write - use AX = 3D02h instead of 3D00h.

    Next run your CRC until end of file - AX returns 0000 in READ: loop. Finish off by inverting it (as at FINI:)

    Next call Int 21 fn 42 (Move File Pointer) with CX:DX = 0 and AL=2 to specify a zero offset from the end of the file.

    Next point DS:DX at your calculated CRC, set CX=4 (number of bytes) and call Int 21 fn 40 to write to the file.

    Finally close the file. If you don't, your new CRC is not saved.

    This is the Autodin-II CRC, by the way. Thanks for the code - the table will be most useful.

  • jeffleydajeffleyda Posts: 390Member
    : : The 'buf db 0' being hardcoded in the program doesn't have any effect on DOS programs since all extra memory is allocated to programs when running under DOS, it won't be overwriting anything important.
    :
    : That's not entirely true! Only COM files have that "privilege", and it's dangerous even so. Running out beyond the end of an EXE file will overwrite the DOS memory pointer chain and cause a crash when the program exits. The EXE header may SAY the program should be allocated all memory, but DOS only gives it the actual size.


    Whoa. This is opposite everything I have ever read and thought I understood about how programs are loaded under DOS.

    So DOS is ignoring the FFFF default memory request in the .exe header?
    Why bother linking with /CP:1 if this value is ignored?


    Why do I still have to deallocate all unused memory if DOS didn't allocate it to my program? What is that memory allocated to if it's not given to the loaded program?



    To be honest, I've always just included my memory deallocate/allocate routines whenever I'm building an .exe file, so I've skirted this issue for long enough time that my memory of how it all works might be fading.

    I did however find this usenet post, so it appears that I'm not the only one who thought the way I do (or did, not sure if you've changed my mind or not!) ;)

    http://tinyurl.com/2etra

    -jeff!
  • peretperet Posts: 69Member
    : Whoa. This is opposite everything I have ever read and thought I understood about how programs are loaded under DOS.
    :
    : So DOS is ignoring the FFFF default memory request in the .exe header?
    : Why bother linking with /CP:1 if this value is ignored?
    :
    : Why do I still have to deallocate all unused memory if DOS didn't allocate it to my program? What is that memory allocated to if it's not given to the loaded program?

    Ok, it's not IGNORING the CPARMALLOC field. If you look at the MCB (memory control block) chain you'll see that each one specifies your EXE as the owner, all the way to the top. However, if you look at the PSP, you find at offset 2 a pointer to the next MCB, which is right at the end of where your EXE loaded, and they chain upwards from there. If you overwrite part of the MCB chain, when your program exits DOS will lock up with an error message and require a reboot.

    Just because your program is allocated the space doesn't mean it can use it. It has to ask first, and if all the memory is allocated - even to itself - the MALLOC call will fail. Hence it has to release memory first, after which it can request the memory you want and DOS will rebuild the MCBs around the new space.

    I know it's illogical. I can only presume it's a bug from an early version of DOS that either got overlooked or grandfathered in for compatibility reasons. It's only an issue for EXE files.

    [code]
    FYI .. Format of the MCB
    offset 0 BLOCKTYPE - 4D not the last, 5A last

    offset 1 OWNER - PSP segment of the EXE program that
    owns the block
    offset 3 SIZE - block size in PARA (16 bytes)

    offset 5 UNUSED - 11 bytes for 16 total
    [/code]




  • descenteracedescenterace Posts: 93Member
    : I know it's illogical. I can only presume it's a bug from an early version of DOS that either got overlooked or grandfathered in for compatibility reasons. It's only an issue for EXE files.

    Actually, COM files have the same problem. I had a hellish time getting a COM database program to allocate memory.
  • peretperet Posts: 69Member
    [b][red]This message was edited by peret at 2004-3-10 12:15:12[/red][/b][hr]
    : Actually, COM files have the same problem. I had a hellish time getting a COM database program to allocate memory.

    Yes .. it's all pretty much undocumented. Here's something I occasionally found useful. Int 21 fn 52 returns a pointer to a list of parameters, one of which is the next MCB address. The segment of the next MCB can be found at ES:[BX]-2

    [code]
    mov ax,5200h
    int 21h
    dec bx
    dec bx
    mov es,es:[bx]
    ; ES:0 is the address of the next MCB
    [/code]

    I've heard, though I can't offer a reference, that DOS allocates all available memory to a COM file. I haven't tried it myself but that operation should point to the end of available memory if you execute it from a COM file.

Sign In or Register to comment.