Intel 8086 assembly reversing order of string - string

So I'm programming code in Intel 8086 assembly where I want to input two strings(one string in one line) and a want to save them to the variables. Each string can contain up to 100 utf8 characters. In the output, I try to change the order of then (the first line is going to be the second string from the input) but I get an error that the service 21h is trying to read from an undefined byte. Can you explain to me what I Should change in my code?
cpu 8086
segment code
..start mov ax, data
mov ds, ax
mov bx,stack
mov ss,bx
mov sp,dno
input1 mov ah,0x0a
mov dx, load
int 21h
mov bl,[load+1]
mov [chars1+bx],byte '$'
input2 mov ah,0x0a
mov dx, load
int 21h
mov bl,[load+1]
mov [chars2+bx],byte '$'
output mov dx,chars2
mov ah,9
int 21h
mov dx,chars1
mov ah,9
int 21h
end hlt
segment data
load db 200, ?
chars1 db ?
resb 100
chars2 db ?
resb 100
segment stack
resb 100
dno: db ?

but I get an error that the service 21h is trying to read from an undefined byte. Can you explain to me what I Should change in my code?
You are not loading the address of the input structure that is required by the DOS.BufferedInput function 0Ah. Read all about it in How buffered input works.
Emu8086 follows MASM programming style where mov dx, load will set the DX register equal to the word stored at the address load. To actually receive the address itself, you'll need to write mov dx, offset load.
A further problem is that both input1 and input2 try to use the same load input structure but with different buffer memories. The buffer has to be part of the input structure for this DOS function; it always has to reside at offset 2 from the provided address (load in your case).
This is how you can solve it:
...
input1: mov ah, 0Ah
mov dx, OFFSET loadA
int 21h
mov bl, [loadA+1]
mov bh, 0
mov [charsA+bx], byte '$'
input2: mov ah, 0Ah
mov dx, OFFSET loadB
int 21h
mov bl, [loadB+1]
mov bh, 0
mov [charsB+bx], byte '$'
output: mov dx, OFFSET charsB
mov ah, 09h
int 21h
mov dx, OFFSET crlf
mov ah, 09h
int 21h
mov dx, OFFSET charsA
mov ah, 09h
int 21h
...
loadA db 101, 0
charsA db 101 dup (0)
loadB db 101, 0
charsB db 101 dup (0)
crlf db 13, 10, "$"
...
101 provides room for 100 characters and 1 terminating carriage return.
Don't trust too much registers that you didn't set yourself! Write an explicite mov bh, 0 before using the whole of BX.
Since you want these strings outputted on different lines, I've added code to move to the start of the following line. See the crlf.

Related

Is there a method to CMP two strings using emu8086?

I am working on a project to organize students mark in 3 exam using assembly language.
I want the emu to CMP the user's string by the ones in the text file, so if ZF set to 1, the emu will print the hole student's information (ID, Full Name, exams marks), that came from the compassion.
Here is the code, I take help from you guys.
ORG 100H
MOV DX, OFFSET MSG1
MOV AH, 9H
INT 21H
MOV DX, OFFSET MSG2
MOV AH, 9H
INT 21H
MOV DX, OFFSET LNBF ; GET STRING FROM USER
MOV AH, 0AH
INT 21H
MOV AL, 0 ; OPEN MY FILE
MOV DX, OFFSET FILE
MOV AH, 3DH
INT 21H
; READ FROM FILE
MOV BX, AX ; MOV HANDLER TO BX
MOV CX, 1 ; READ CHAR ONE BY ONE
LEA DX, DATABF
INT 21H
RET
FILE DB "MY.txt",0
LNBF DB 1EH,?
MSG1 DB "FIND A STUDENT BY HIS/HER LAST NAME:$"
MSG2 DB 0DH,0AH,0DH,0AH,"ENTER THE STUDENT'S LAST NAME->: $"
DATABF DW 0FFFH
Do correct these errors before you continue:
LNBF DB 1EH,? does a bad job setting up a buffer to input the student's name!
It overwrites MSG1 instead of providing a decent dedicated buffer.
The correct way is : LNBF DB 30, 0, 30 dup (0)
For detailed info about the DOS.BufferedInput function 0Ah see
How buffered input works
Your READ FROM FILE code forgets to specify the required function number 3Fh.
Use mov ah, 3Fh. Also you should not neglect the possibility that an error is returned via the carry flag!
Below is an example that you can use. It compares the carriage return-terminated name in the inputbuffer with the zero-terminated name in the text file. (The file could of course be using any string terminator that suits you...)
mov si, offset LNBF + 2 ; -> SI is address of student's name.
More:
call ReadOneCharFromFile ; -> AL
cmp al, 0
je SkipToNextNameInFile
cmp al, [si]
jne SkipToNextNameInFile
inc si
cmp byte [si], 13
jne More
call ReadOneCharFromFile ; -> AL
cmp al, 0
jne SkipToNextNameInFile
MatchFound:
...
SkipToNextNameInFile:
...

8086 Write a program that takes one string from the input and inserts white space between every two letters

8086 Write a program that takes one string from the input and inserts white space between every two letters
using nasm and MSDOS
i have do following code but it is not working
start:
mov ax,data
mov ds, ax
mov es, ax
mov cx, size;cx will contain size,we need it to
; check if we have got 10 inputs from key board
lea dx, Enter_string;it will display a text on screen to enter text
mov ah, 9
int 21h
call get_string;input string from keyboard
mov ax, 4ch;terminating
int 21h
get_string:
mov si, 0; si will be used as index
mov bx, offset string
get_char:
mov ah, 1; get a char from keyboard
int 21h
mov [bx][si], al; saving input in string
inc si
cmp si,cx;if si=7 than, no need to take more input
jne get_char
ret
. You should prefer the simplicity of .COM programs while learning. They already start with all the segment registers pointing to the program.
. The DOS exit function expects the function number in AH.
. On NASM you don't use mov bx, offset string. Just write mov bx, string.
. It's easy to combine the tasks of inputting a string and interjecting space characters. See below code:
org 256 ;.COM programs have CS=DS=ES=SS
start:
mov cx, size ;cx will contain size,we need it to
; check if we have got 10 inputs from key board
mov dx, Enter_string ;it will display a text on screen to enter text
mov ah, 9
int 21h
call get_string ;input string from keyboard
mov ax, 4C00h ;terminating
int 21h
get_string:
push cx
mov si, 0 ; si will be used as index
get_char:
mov ah, 1 ; get a char from keyboard
int 21h
mov ah, " "
mov [string+si], ax ; saving input in string PLUS THE SPACE CHARACTER
add si, 2
dec cx ;if si=7 than, no need to take more input
jnz get_char
pop cx
ret
Remember that you don't actually need the last space character. Just overwrite it with the string terminator that you normally would add to this string!

Getting string input and displaying input with DOS interrupts MASM

In MASM, I created a buffer variable to hold the user string input from keyboard. I am stuck on how to hold the string input into that buffer variable. I don't have any libraries linked like the irvine ones and want to do this with DOS interrupts. So far I have something along the lines of
.model small
.stack 100h
.data
buff db 25 dup(0), 10, 13
lbuff EQU ($ - buff) ; bytes in a string
.code
main:
mov ax, #data
mov ds, ax
mov ah, 0Ah ; doesn't work
mov buff, ah ; doesn't seem right
int 21h
mov ax, 4000h ; display to screen
mov bx, 1
mov cx, lbuff
mov dx, OFFSET buff
int 21h
mov ah, 4ch
int 21h
end main
I assume using 0Ah is correct as it is for reading array of input of buffered characters.
I made some changes to your code. First, the "buff" variable needs the three level format (max number of characters allowed, another byte for the number of characteres entered, and the buffer itself) because that's what service 0AH requires. To use service 0AH I added "offset buff" (as Wolfgang said). Here it is:
.model small
.stack 100h
.data
buff db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;NUMBER OF CHARACTERS ENTERED BY USER.
db 26 dup(0) ;CHARACTERS ENTERED BY USER.
.code
main:
mov ax, #data
mov ds, ax
;CAPTURE STRING FROM KEYBOARD.
mov ah, 0Ah ;SERVICE TO CAPTURE STRING FROM KEYBOARD.
mov dx, offset buff
int 21h
;CHANGE CHR(13) BY '$'.
mov si, offset buff + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, '$'
mov [ si ], al ;REPLACE CHR(13) BY '$'.
;DISPLAY STRING.
mov ah, 9 ;SERVICE TO DISPLAY STRING.
mov dx, offset buff + 2 ;MUST END WITH '$'.
int 21h
mov ah, 4ch
int 21h
end main
When 0AH captures the string from keyboard, it ends with ENTER (character 13), that's why, if you want to capture 25 characters, you must specify 26.
To know how many characters the user entered (length), access the second byte (offset buff + 1). The ENTER is not included, so, if user types 8 characters and ENTER, this second byte will contain the number 8, not 9.
The entered characters start at offset buff + 2, and they end when character 13 appears. We use this to add the length to buff+2 + 1 to replace chr(13) by '$'. Now we can display the string.
This is my code,maybe can help you.
;Input String Copy output
dataarea segment
BUFFER db 81
db ?
STRING DB 81 DUP(?)
STR1 DB 10,13,'$'
dataarea ends
extra segment
MESS1 DB 'After Copy',10,13,'$'
MESS2 DB 81 DUP(?)
extra ends
code segment
main proc far
assume cs:code,ds:dataarea,es:extra
start:
push ds
sub ax,ax
push ax
mov ax,dataarea
mov ds,ax
mov ax,extra
mov es,ax
lea dx,BUFFER
mov ah,0ah
int 21h
lea si,STRING
lea di,MESS2
mov ch,0
mov cl,BUFFER+1
cld
rep movsb
mov al,'$'
mov es:[di],al
lea dx,STR1 ;to next line
mov ah,09h
int 21h
push es
pop ds
lea dx,MESS1 ;output:after copy
mov ah,09h
int 21h
lea dx,MESS2
mov ah,09h
int 21h
ret
main endp
code ends
end start
And the result is:
c:\demo.exe
Hello World!
After Copy
Hello World!
You may follow this code :
; Problem : input array from user
.MODEL SMALL
.STACK
.DATA
ARR DB 10 DUB (?)
.CODE
MAIN PROC
MOV AX, #DATA
MOV DS, AX
XOR BX, BX
MOV CX, 5
FOR:
MOV AH, 1
INT 21H
MOV ARR[BX], AL
INC BX
LOOP FOR
XOR BX, BX
MOV CX, 5
PRINT:
MOV AX, ARR[BX] ;point to the current index
MOV AH, 2 ;output
MOV DL, AX
INT 21H
INC BX ;move pointer to the next element
LOOP PRINT ;loop until done
MAIN ENDP
;try this one, it takes a 10 character string input from user and displays it after in this manner, "Hello *10character string input"
.MODEL TINY
.CODE
.286
ORG 100h
START:
MOV DX, OFFSET BUFFER
MOV AH, 0ah
INT 21h
JMP PRINT
BUFFER DB 10,?, 10 dup(' ')
PRINT:
MOV AH, 02
MOV DL, 0ah
INT 21h
MOV AH, 9
MOV DX, OFFSET M1
INT 21h
XOR BX, BX
MOV BL, BUFFER[1]
MOV BUFFER [BX+2], '$'
MOV DX, OFFSET BUFFER +2
MOV AH, 9
INT 21h
M1: db 'Hello $'
END START
END

Sorting strings in 8086 Assembly

I want to write a 8086 assembly program that takes 5 strings from the user as an input and then sorts these strings and prints the sorted result as an output. I actually do everything but I have a big problem with the sorting part. I know how to use a for example bubble sort to sort the items in an array that start from a specific address but here I have 5 different strings that are not in the same array. each string has its own address and its own characters. I try to compare last character of each string with each other and then if one is bigger that another one i swap the whole string and then I go on and do that for the whole characters of all string to the first.
For example if our input strings are:
eab
abe
cbd
cda
adb
I will first sort the last character of every string and I come up with this:
cda
eab
adb
cbd
abe
Then I will compare them by the middle character:
eab
cbd
abe
cda
adb
and at last with the first character and everything is sorted:
abe
adb
cbd
cda
eab
but it is actually what in my mind and I don't have any idea who to implement that for my job.
; multi-segment executable file template.
data segment
data1 db 64,?,64 dup(?)
data2 db 64,?,64 dup(?)
data3 db 64,?,64 dup(?)
data4 db 64,?,64 dup(?)
data5 db 64,?,64 dup(?)
change db 66 dup(?)
msg db 0ah,0dh,"You enter a wrong option",0ah,0dh,"try again",0ah,0dh,"$"
prompt db 0ah,0dh,"Choose an option:",0ah,0dh,"$"
prompt1 db ".a: Sort in ascending order",0ah,0dh,"$"
prompt2 db ".d: Sort in descending order",0ah,0dh,"$"
prompt3 db ".q: Quit",0ah,0ah,0dh,"$"
enter db 0ah,0ah,0dh,"Enter 5 strings:",0ah,0dh,"$"
pkey db 0ah,0dh,"press any key...$"
ends
stack segment
dw 128 dup(0)
ends
code segment
main proc far
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
again:
; printing the prompts for the user
lea dx, prompt
mov ah, 09h
int 21h
lea dx, prompt1
mov ah, 09h
int 21h
lea dx, prompt2
mov ah, 09h
int 21h
lea dx, prompt3
mov ah, 09h
int 21h
; getting a character from the user as an input
mov ah, 01h
int 21h
; determining which option the user selects
cmp al, 'a'
je ascending
cmp al, 'd'
je descending
cmp al, 'q'
je quit
; this is for the time that the user enters a wrong char
lea dx, msg
mov ah, 09h
int 21h
jmp again ; again calling the application to start
ascending:
call input
call AscendSort
jmp again ; again calling the application to start
descending:
call input
call DescendSort
jmp again ; again calling the application to start
quit:
lea dx, pkey
mov ah, 9
int 21h ; output string at ds:dx
; wait for any key....
mov ah, 1
int 21h
mov ax, 4c00h ; exit to operating system.
int 21h
main endp
;.................................................
; this subroutine gets input from user
input proc
lea dx, enter
mov ah, 09h
int 21h
call newline
mov ah, 0ah
lea dx, data1
int 21h
call newline
mov ah, 0ah
lea dx, data2
int 21h
call newline
mov ah, 0ah
lea dx, data3
int 21h
call newline
mov ah, 0ah
lea dx, data4
int 21h
call newline
mov ah, 0ah
lea dx, data2
int 21h
call newline
ret
input endp
;................................................
; sorting the strings in the ascending order
AscendSort proc
mov si, 65
lea dx, change
mov al, data1[si]
cmp al, data2[si]
ja l1
?????
ret
AscendSort endp
;................................................
; sorting the strings in the descending order
DescendSort proc
ret
DescendSort endp
;................................................
; newline
newline proc
mov ah, 02h
mov dl, 0ah
int 21h
mov dl, 0dh
int 21h
ret
newline endp
ends
end main ; set entry point and stop the assembler.
Any other algorithm for sorting these whole strings also will be appreciated.
I actually figure out the answer myself, I use string commands to compare the strings 2 by 2 with each other to see if they're bigger, smaller or equal. Something like the code below in the specific macro that takes two strings to check them and do the required operation like swapping the strings to make them sorted:
check macro a, b
local next, finish
cld
mov cx, 64 ; the size of our buffer that saves the string
mov si, a
mov di, b
repe cmpsb ; comparing two strings with each other
ja next
jmp finish
next:
; swaping our strings if needed
mov cx, 64
mov si, a
lea di, change
rep movsb
mov cx, 64
mov si, b
mov di, a
rep movsb
mov cx, 64
lea si, change
mov di, b
rep movsb
finish:
endm

search for string in file in ASSEMBLY

I need to write a program in ASSEMBLY to search for a string (input for keyboard) in all file of current folder.
What's interrupt can I use and what's algorithm of this program?
Hmm...here's some code I wrote (at least I think I wrote it -- it looks like code I'd have written, though it doesn't have any comments to confirm it) to list the files in a directory:
.model small, c
.stack
.data
file_spec db "*.*", 0
DTA db 128h dup(0)
buffer db 30 dup(0)
.code
main proc
mov ax, #Data
mov ds, ax
mov dx,offset DTA
mov ah,1Ah
int 21h ; set DTA location
mov dx,offset file_spec
xor cx, cx
mov ah,4Eh
int 21h
jc quit
print_name:
lea si, DTA + 30
next_char:
lodsb
int 29h
test al, al
jnz next_char
mov al, 13
int 29h
mov al, 10
int 29h
mov dx, offset file_spec
xor cx, cx
mov ah, 4fh
int 21h
jnc print_name
quit:
mov ax, 4c00h
int 21h
main endp
end main
Here's another that opens and displays a file:
data segment para public 'DATA'
line db 80 dup(?)
file db "test.txt"
handle dw ?
data ends
code segment para public 'CODE'
assume ds:data
assume cs:code
main proc
mov ax,data
mov ds,ax
; okay. First, open the file
mov dx,offset file
mov ax,3d00h
int 21h
mov handle,ax
read_loop:
mov dx,offset line
mov bx,handle
mov cx, 80 ; the size of our buffer.
mov ah,3fh
int 21h
jc done
test ax,ax
jz done
mov bx,1
mov cx,ax
mov dx,offset line
mov ah,40h
int 21h
jmp read_loop
done:
mov ax,4c00h
int 21h
main endp
code ends
end main
Searching for the data in the file would be basically a matter of writing a strstr in assembly language. You'd probably want to start with a quick scan using rep cmpsb and then do a byte-by-byte comparison when you find a candidate match. If that fails, you increment the position and try again.

Resources