Use backtick code blocks for syntax highlighting
This commit is contained in:
parent
8ed9dea394
commit
f658bfbb40
203 changed files with 26546 additions and 25801 deletions
188
01-03.md
188
01-03.md
|
|
@ -30,105 +30,109 @@ disk caching turned off.
|
|||
|
||||
**LISTING 1.2 L1-2.C**
|
||||
|
||||
/*
|
||||
* Program to calculate the 16-bit checksum of the stream of bytes
|
||||
* from the specified file. Obtains the bytes one at a time in
|
||||
* assembler, via direct calls to DOS.
|
||||
*/
|
||||
```c
|
||||
/*
|
||||
* Program to calculate the 16-bit checksum of the stream of bytes
|
||||
* from the specified file. Obtains the bytes one at a time in
|
||||
* assembler, via direct calls to DOS.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
main(int argc, char *argv[]) {
|
||||
int Handle;
|
||||
unsigned char Byte;
|
||||
unsigned int Checksum;
|
||||
int ReadLength;
|
||||
main(int argc, char *argv[]) {
|
||||
int Handle;
|
||||
unsigned char Byte;
|
||||
unsigned int Checksum;
|
||||
int ReadLength;
|
||||
|
||||
if ( argc != 2 ) {
|
||||
printf("usage: checksum filename\n");
|
||||
exit(1);
|
||||
}
|
||||
if ( (Handle = open(argv[1], O_RDONLY | O_BINARY)) == -1 ) {
|
||||
printf("Can't open file: %s\n", argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
if ( !ChecksumFile(Handle, &Checksum) ) {
|
||||
printf("Error reading file %s\n", argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
if ( argc != 2 ) {
|
||||
printf("usage: checksum filename\n");
|
||||
exit(1);
|
||||
}
|
||||
if ( (Handle = open(argv[1], O_RDONLY | O_BINARY)) == -1 ) {
|
||||
printf("Can't open file: %s\n", argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
if ( !ChecksumFile(Handle, &Checksum) ) {
|
||||
printf("Error reading file %s\n", argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Report the result */
|
||||
printf("The checksum is: %u\n", Checksum);
|
||||
exit(0);
|
||||
}
|
||||
/* Report the result */
|
||||
printf("The checksum is: %u\n", Checksum);
|
||||
exit(0);
|
||||
}
|
||||
```
|
||||
|
||||
**LISTING 1.3 L1-3.ASM**
|
||||
|
||||
; Assembler subroutine to perform a 16-bit checksum on the file
|
||||
; opened on the passed-in handle. Stores the result in the
|
||||
; passed-in checksum variable. Returns 1 for success, 0 for error.
|
||||
;
|
||||
; Call as:
|
||||
; int ChecksumFile(unsigned int Handle, unsigned int *Checksum);
|
||||
;
|
||||
; where:
|
||||
; Handle = handle # under which file to checksum is open
|
||||
; Checksum = pointer to unsigned int variable checksum is
|
||||
; to be stored in
|
||||
;
|
||||
; Parameter structure:
|
||||
;
|
||||
Parms struc
|
||||
dw ? ;pushed BP
|
||||
dw ? ;return address
|
||||
Handle dw ?
|
||||
Checksum dw ?
|
||||
Parms ends
|
||||
;
|
||||
.model small
|
||||
.data
|
||||
TempWord label word
|
||||
TempByte db ? ;each byte read by DOS will be stored here
|
||||
db 0 ;high byte of TempWord is always 0
|
||||
;for 16-bit adds
|
||||
;
|
||||
.code
|
||||
public _ChecksumFile
|
||||
_ChecksumFile proc near
|
||||
push bp
|
||||
mov bp,sp
|
||||
push si ;save C's register variable
|
||||
;
|
||||
mov bx,[bp+Handle] ;get file handle
|
||||
sub si,si ;zero the checksum ;accumulator
|
||||
mov cx,1 ;request one byte on each ;read
|
||||
mov dx,offset TempByte ;point DX to the byte in
|
||||
;which DOS should store
|
||||
;each byte read
|
||||
ChecksumLoop:
|
||||
mov ah,3fh ;DOS read file function #
|
||||
int 21h ;read the byte
|
||||
jcErrorEnd;an error occurred
|
||||
and ax,ax ;any bytes read?
|
||||
jz Success ;no-end of file reached-we're done
|
||||
add si,[TempWord] ;add the byte into the
|
||||
;checksum total
|
||||
jmpChecksumLoop
|
||||
ErrorEnd:
|
||||
sub ax,ax ;error
|
||||
jmp short Done
|
||||
Success:
|
||||
mov bx,[bp+Checksum] ;point to the checksum variable
|
||||
mov [bx],si ;save the new checksum
|
||||
mov ax,1 ;success
|
||||
;
|
||||
Done:
|
||||
pop si ;restore C's register variable
|
||||
pop bp
|
||||
ret
|
||||
_ChecksumFileendp
|
||||
end
|
||||
```nasm
|
||||
; Assembler subroutine to perform a 16-bit checksum on the file
|
||||
; opened on the passed-in handle. Stores the result in the
|
||||
; passed-in checksum variable. Returns 1 for success, 0 for error.
|
||||
;
|
||||
; Call as:
|
||||
; int ChecksumFile(unsigned int Handle, unsigned int *Checksum);
|
||||
;
|
||||
; where:
|
||||
; Handle = handle # under which file to checksum is open
|
||||
; Checksum = pointer to unsigned int variable checksum is
|
||||
; to be stored in
|
||||
;
|
||||
; Parameter structure:
|
||||
;
|
||||
Parms struc
|
||||
dw ? ;pushed BP
|
||||
dw ? ;return address
|
||||
Handle dw ?
|
||||
Checksum dw ?
|
||||
Parms ends
|
||||
;
|
||||
.model small
|
||||
.data
|
||||
TempWord label word
|
||||
TempByte db ? ;each byte read by DOS will be stored here
|
||||
db 0 ;high byte of TempWord is always 0
|
||||
;for 16-bit adds
|
||||
;
|
||||
.code
|
||||
public _ChecksumFile
|
||||
_ChecksumFile proc near
|
||||
push bp
|
||||
mov bp,sp
|
||||
push si ;save C's register variable
|
||||
;
|
||||
mov bx,[bp+Handle] ;get file handle
|
||||
sub si,si ;zero the checksum ;accumulator
|
||||
mov cx,1 ;request one byte on each ;read
|
||||
mov dx,offset TempByte ;point DX to the byte in
|
||||
;which DOS should store
|
||||
;each byte read
|
||||
ChecksumLoop:
|
||||
mov ah,3fh ;DOS read file function #
|
||||
int 21h ;read the byte
|
||||
jcErrorEnd;an error occurred
|
||||
and ax,ax ;any bytes read?
|
||||
jz Success ;no-end of file reached-we're done
|
||||
add si,[TempWord] ;add the byte into the
|
||||
;checksum total
|
||||
jmpChecksumLoop
|
||||
ErrorEnd:
|
||||
sub ax,ax ;error
|
||||
jmp short Done
|
||||
Success:
|
||||
mov bx,[bp+Checksum] ;point to the checksum variable
|
||||
mov [bx],si ;save the new checksum
|
||||
mov ax,1 ;success
|
||||
;
|
||||
Done:
|
||||
pop si ;restore C's register variable
|
||||
pop bp
|
||||
ret
|
||||
_ChecksumFileendp
|
||||
end
|
||||
```
|
||||
|
||||
The lesson is clear: Optimization makes code faster, but without proper
|
||||
design, optimization just creates fast slow code.
|
||||
|
|
|
|||
Loading…
Reference in a new issue