Home > other >  How to read a string separate by symbol in assembly language masm?
How to read a string separate by symbol in assembly language masm?

Time:09-20

I recently watched a tutorial on how to read string in assembly language masm 8086.
I want to write this into my file called 'TEST.txt'.
Below is the text

Ali|RM1000
Abu|RM1500

but I only can print out this

Ali|RM1000
Abu|RM1500
Abu|RM1500

The name 'Abu' and 'RM1500' got printed out twice in the txt file. I also wanted to ask whether I can read the string in assembly just like in java, using split("\|"). I would appreciate anyone helping me with this.

.MODEL SMALL
.STACK 100H
.DATA
FNAME DB 'TEST.TXT',0
HANDLE DW ?
MSG DB 'Ali|RM1000', 13, 10
MSG2 DB 'Abu|RM1500', 13, 10
BUFFER DB 25 DUP(?)
COUNT DB 10
.CODE 
MAIN PROC
MOV AX,@DATA
MOV DS,AX

;***************OPENING A NEW FILE***************;

; MOV AH,3CH
; MOV DX,OFFSET(FNAME)
; MOV CL,1
; INT 21H
; MOV HANDLE,AX


;***************CLOSEING A FILE***************;
; MOV AH,3EH
; MOV DX,HANDLE
; INT 21H

;***************OPENING AN EXISTING FILE***************;

MOV AH,3DH
MOV DX,OFFSET(FNAME)
MOV AL,1  ; 1 MEAN FOR WRITING PURPOSE 
INT 21H
MOV HANDLE,AX

MOV AH,40H
MOV BX,HANDLE
MOV CX,25
MOV DX, OFFSET(MSG)
INT 21H

MOV AH,40H
MOV BX,HANDLE
MOV CX,25
MOV DX, OFFSET(MSG2)
INT 21H

MOV AH,3EH
MOV DX,HANDLE
INT 21H

;***************OPENING READING FILE***************;
MOV AH,3DH
MOV DX,OFFSET(FNAME)
MOV AL,0  ; 1 MEAN FOR WRITING PURPOSE 
INT 21H
MOV HANDLE,AX

MOV AH,3FH
MOV BX,HANDLE
MOV DX,OFFSET(BUFFER)
MOV CX,10
INT 21H

;***************DISPLAYING 10 BYTES***************;
MOV SI,OFFSET(BUFFER)
L1:
MOV AH,2
MOV DL,[SI]
INT 21H
INC SI
DEC COUNT
JNZ L1

MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

CodePudding user response:

MSG DB 'Ali|RM1000', 13, 10
MSG2 DB 'Abu|RM1500', 13, 10
BUFFER DB 25 DUP(?)

You are opening an existing file for output and then you write 25 bytes to it, twice. Your file's first 50 bytes will be:

'Ali|RM1000', 13, 10, 'Abu|RM1500', 13, 10, 0
'Abu|RM1500', 13, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
MOV AH, 3Eh
MOV DX, HANDLE
INT 21h

The handle must go in the BX register!

Apply these corrections:

MOV DX, OFFSET(FNAME)
MOV AX, 3D01h         ; DOS.OpenFile
INT 21h               ; -> AX CF
JC  AnErrorOccured    ; What if ?
MOV HANDLE, AX        ; Probably a redundant store !

MOV DX, OFFSET(MSG)
MOV CX, 10   2        ; Length of the text including 13, 10
MOV BX, AX            ; HANDLE
MOV AH, 40h           ; DOS.WriteFile
INT 21h               ; -> AX CF
JC  AnErrorOccured    ; What if ?
CMP AX, CX
JNE AnErrorOccured    ; What if ?

MOV DX, OFFSET(MSG2)  ; No need to repeat setting CX and BX
MOV AH, 40h           ; DOS.WriteFile
INT 21h               ; -> AX CF
JC  AnErrorOccured    ; What if ?
CMP AX, CX
JNE AnErrorOccured    ; What if ?

MOV AH, 3Eh           ; DOS.CloseFile
INT 21h               ; -> AX CF
                      ; Don't care about CF
  • Related