I have some errors in this program, and I could not fix them. I need help to fix
ID: 3860792 • Letter: I
Question
I have some errors in this program, and I could not fix them. I need help to fix this program.
; The results are printed to the screen
; Uses READ, WRITE and SWAP macros
;
; Define constants and Macros
;
CR EQU 0DH ;define carriage return
LF EQU 0DH ;define line feed
EOT EQU ' $ ' ;define end of text marker of INT 21H
NULL EQU 00 ;NULL character
SPACE EQU 20H ;blank character
READ MACRO
LEA DX, #1 ;point to input buffer
MOV AH, 0AH ;set function code
INT 21H ;call system I/O
#EM
WRITE MACRO
LEA DX, #1 ;point to output text string
MOV AH, 09H ;set function code
INT 21H ;call system I/O
#EM
SWAP MACRO
MOV BH, [ SI ] ;save first byte
MOV BL, [ SI+1 ] ;save second byte
MOV [ SI ], BL ;swap bytes
MOV [ SI+1 ], BH
#EM
EXIT MACRO
MOV AX, 4C00H ;exit clean code
INT 21H ;call system function
#EM
;
;
JMP START
;
;
; Define Variables and Strings
;
MSG1 DB CR, LF, "Enter up to 100 arbitrary characters : ", EOT
MSG2 DB CR, LF, LF, "The string as sorted is", CR, LF, EOT
INBUF DB 100, ?, 100 DUP ?
MSG3 DB CR, LF, LF, "Shall we do it again? ", EOT
;
; Start Code Section
START: CALL CLS ;clear screen for this run
WRITE MSG1 ;prompt for string
READ INBUF ;get sring
LEA SI, INBUF+2 ;point to start of data
CALL BSORT ;sort data
LEA SI, INBUF+2 ;point to data
MOV BL, [ SI -1 ] ;get count
MOV BH, 0
MOV B [ BX + SI ], EOT ;mark end of string
WRITE MSG2 ;write header message
LEA SI, INBUF+2 ;write sorted data
CALL OUTPUT
WRITE MSG3 ;ask for repeat
MOV AH, 01H ;get response
INT 21H
OR AL, 00100000B ;make input lower case
CMP AL, 'y' ;is it yes?
JE START ;it is, do it again
EXIT ;no, so exit
;
;************************************************************************************************************************
; Subtroutines for this program
;************************************************************************************************************************
; Subtroutine CLS
;
; Clears Screen
;
CLS: MOV AX, 0600H ;Set function code for scroll down
MOV BH, 07 ;Select active page
MOV CX, 0000H ;Start location
MOV DX, 184FH ;End location
INT 10H ;Call BIOS
MOV AH, 02H ;Cursor set function
MOV BH, 00 ;Select page
MOV DX, 0000 ;Cursor coordinates
INT 10H ;Call BIOS
RET
;
; Subroutine BSORT
;
; Sorts a string of bytes in ascending order
;
; ENTRY: SI points to the sting to be sorted
; String ends with CR, count is in [ SI-1 ]
; EXIT: SI modified
BSORT: MOV DI, SI ;save pointer to top of string
MOV CL, [SI-1] ;get string size
DEC CL ;set outer loop count
BS0: MOV CH, CL ;set inner loop count
MOV SI, DI ;get pointer to top of string
BS1: MOV AL, [SI] ;get a character
CMP AL, [SI+1] ;test against next character
JNAE BS2 ;character not out of order
SWAP ;swap if not in order
BS2: INC SI ;point to next pair
DEC CH ;decrement inner loop count
JNZ BS1 ;not at end of string yet
DEC CL ;at end, decrement outer loop count
JNZ BS0 ;not done, do another loop
RET
;
; Subroutine OUTPUT
;
; This subroutine formats the string output by insertung spa
; between the characetrs and after printing 20 characters
; inserts a new line
;
; Entry: SI points to start of string
; Exit: Nothing special
OUTPUT:
MOV CL, 20 ;set maximum charactersper line
OO1: MOV DL, [SI] ;get characters
CMP DL, CR ;at end of string?
JE OO2 ;yep, get out
MOV AH, 02H ;no, function for single char out
INT 21H
MOV DL, SPACE ;write a space
INT 21H
INC SI ;point to next character
DEC CL ;count down output
JNZ OO1 ;continue printing
MOV DL, CR ;we wrote 20, insert new line
INT 21H
MOV DL, LF
INT 21H
JMP OUTPUT
OO2: RET
;
END
Explanation / Answer
;
;
; Define Variables and Strings
;
MSG1 DB CR, LF, "Enter up to 100 arbitrary characters : ", EOT
MSG2 DB CR, LF, LF, "The string as sorted is", CR, LF, EOT
INBUF DB 100, ?, 100 DUP ?
MSG3 DB CR, LF, LF, "Shall we do it again? ", EOT
;
; Start Code Section
START: CALL CLS ;clear screen for this run
WRITE MSG1 ;prompt for string
READ INBUF ;get sring
LEA SI, INBUF+2 ;point to start of data
CALL BSORT ;sort data
LEA SI, INBUF+2 ;point to data
MOV BL, [ SI -1 ] ;get count
MOV BH, 0
MOV B [ BX + SI ], EOT ;mark end of string
WRITE MSG2 ;write header message
LEA SI, INBUF+2 ;write sorted data
CALL OUTPUT
WRITE MSG3 ;ask for repeat
MOV AH, 01H ;get response
INT 21H
OR AL, 00100000B ;make input lower case
CMP AL, 'y' ;is it yes?
JE START ;it is, do it again
EXIT ;no, so exit
;
;************************************************************************************************************************
; Subtroutines for this program
;************************************************************************************************************************
; Subtroutine CLS
;
; Clears Screen
;
CLS: MOV AX, 0600H ;Set function code for scroll down
MOV BH, 07 ;Select active page
MOV CX, 0000H ;Start location
MOV DX, 184FH ;End location
INT 10H ;Call BIOS
MOV AH, 02H ;Cursor set function
MOV BH, 00 ;Select page
MOV DX, 0000 ;Cursor coordinates
INT 10H ;Call BIOS
RET
;
; Subroutine BSORT
;
; Sorts a string of bytes in ascending order
;
; ENTRY: SI points to the sting to be sorted
; String ends with CR, count is in [ SI-1 ]
; EXIT: SI modified
BSORT: MOV DI, SI ;save pointer to top of string
MOV CL, [SI-1] ;get string size
DEC CL ;set outer loop count
BS0: MOV CH, CL ;set inner loop count
MOV SI, DI ;get pointer to top of string
BS1: MOV AL, [SI] ;get a character
CMP AL, [SI+1] ;test against next character
JNAE BS2 ;character not out of order
SWAP ;swap if not in order
BS2: INC SI ;point to next pair
DEC CH ;decrement inner loop count
JNZ BS1 ;not at end of string yet
DEC CL ;at end, decrement outer loop count
JNZ BS0 ;not done, do another loop
RET
;
; Subroutine OUTPUT
;
; This subroutine formats the string output by insertung spa
; between the characetrs and after printing 20 characters
; inserts a new line