Taking input string from keyboard and outputting a string in Assembly (8086) - string

I am trying to create a program that greets the user with their name. The user enters their name after a prompt and the they are greeted along with their name. I have already tried using this code but it fails and throws error on line 21.
The error message states:
(21) wrong parameters: LEA Dx,name
(21) probably no zero prefix for hex; or no 'h' suffix; or wrong addressing; or undefined var: name
Here's the code
.MODEL SMALL
.STACK 100
.DATA
msg db "Hello! Please enter your name:$"
newline db 13,10,'$'
greeting db "Wellcome!$"
name db 80, 0, 78 DUP('$')
.CODE
main PROC
; Prompt
MOV Ax,#DATA
MOV Ds,Ax
LEA Dx,msg
MOV Ah,09h
INT 21h
; Input
LEA Dx,name (throws error)
MOV AH,0AH
INT 21h
; Check if ENTER is pressed
CMP Al,13
JE Display
; Newline
LEA Dx,newline
MOV Ah,09h
INT 21h
; Print Greeting
Display: MOV AH,09h
LEA Dx,name+2
;MOV Dl,Al
INT 21H
Exit:
MOV Ah,4Ch
INT 21h
main ENDP
END main

it fails is not a useful description. When you want to use Int 21/AH=0Ah buffered input, the first buffer byte has to be initialized with its size, for instance
name db 80, 0, 78 DUP('$')
Also remove the instruction MOV Dl,Al which damages DX in ; Print Greeting.

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:
...

Assembly Language: Completing the program to get the midstring

.model small
.stack
.data
msg1 db "Enter string max of 9 characters: $"
msg2 db 13,10, "Enter a number: $"
msg3 db 13,10, "Midstring: $"
strNine db "$"
num db 0,"$"
mid db "$"
varName label byte
maxL db 10
actL db 0
actCont db 10 dup("?")
.code
mov ax,#data
mov ds,ax
;-------------------- Input String ---------------------
mov ah,9
lea dx,msg1
int 21h
mov ah,0ah
int 21h
mov strNine,al
mov bh,strNine
;-------------------- Number ---------------------
mov ah,9
lea dx,msg2
int 21h
mov ah,1
int 21h
mov num,al
mov bl,num
;-------------------- Midstring ---------------------
mov ah,9
lea dx,msg3
int 21h
mov ah,4ch
int 21h
END
I need to find the midstring and I'm stuck since I'm new to Assembly Language.
Expected behaviour:
Enter max of 9 String: helloword
Enter a number: 3
Midstring: lloword
The leading space, the h and e shall be deleted because of the number input by the user.
The only part the I've gotten is to get the input from the user which is the string and number which i have saved in the BX memory which is used for indexing
I just need some tips/guides from you guys to finish the program.
mov ah,9
lea dx,msg1
int 21h
mov ah,0ah
int 21h
How can this input work at all? At the moment that you call the DOS input function your DX register is still set for the msg1 when it should be set to point to the varName input structure.
mov ah,0ah
int 21h
mov strNine,al
mov bh,strNine
What do you expect the AL register to hold at this point? This DOS function doesn't store a useful value there!

String buffer in assembly code

I just have ended my code which allow to cover your password. It goes like this(FASM):
org 100h
mov cx, 16
petla:
mov ah,08h
int 21h
cmp al,0dh
je OK
mov ah,02h
mov dl,42
int 21h
cmp cx,0
je Fail
loop petla
Fail:
mov dl, 0ah
int 21h
mov dx, pass2
mov ah,9
int 21h
jmp koniec
OK:
mov dl, 0ah
int 21h
mov dx, pass
mov ah,9
int 21h
jmp koniec
koniec:
mov ah,4ch
int 21h
pass db 'Password OK', 0Ah, 0Dh, '$'
pass2 db 'Password Fail', 0Ah, 0Dh, '$'
And now I need to print the genuine password. I know the string buffer is a must and how declaration of the buffer should look like but I don't really know how to use it and make it work.
Calling for help :)
Cheers.
Since your program allows the input of 15 characters of password, you could setup the buffer with:
Buffer db 16 dup ("$")
You initialize the DI register before your petla loop and put the ASCII code you got from the DOS function in the buffer via a stosb instruction:
mov di, Buffer
mov cx, 16
petla:
mov ah,08h
int 21h
cmp al,0dh
je OK
stosb
mov ah,02h
mov dl,42
int 21h
;;;cmp cx,0
;;;je Fail
loop petla
Please note that compairing for CX=0 is useless just before the loop instruction in your code.

String assembly

The task is to search a string and find if inputted char is in the string MES3 or not.Here is my code , but it doesn`t search all letters in the string just the first .How can i make the cycle to work and search through all symbols in the string
masm
model small
.DATA
MSG1 DB 10,13,'CHARACTER FOUND :) $'
MSG2 DB 10,13,'CHARACTER NOT FOUND :($'
MSG3 DB 10,13,'there is no hope of doing this bla : $'
MSG4 DB 10,13,'ENTER THE CHARACTER TO BE SEARCHED : $'
NEW DB 10,13,'$'
NEW1 DB 10,13,'$'
NEW2 DB 10,13,'$'
.CODE
ASSUME CS:#CODE,DS:#DATA
START:
MOV AX,#DATA
MOV DS,AX
LEA di,[MSG3]
DOWN:
LEA dx,NEW
MOV AH,09H
INT 21H
LEA DX,MSG4
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
MOV DI,0
UP1:
CMP AL,[MSG3+di]
JE DOWN3
INC DI
LOOP UP1
LEA DX,MSG2
MOV AH,09H
INT 21H
JMP FINISH
DOWN3:
LEA DX,MSG1
MOV AH,09H
INT 21H
FINISH:
INT 3
mov AX, 4c00h
int 21h
END START
The LOOP instruction (that you use in LOOP UP1) decrements CX and jumps to the target label if CX != 0. So you need to set CX to the maximum number of characters to compare before the UP1 label.
Or you could replace LOOP UP1 with CMP BYTE PTR [MSG3-1+di],'$' / JNE UP1 since the string is '$'-terminated.

x86 assembly program to search for a word in a given text

I am trying to write a program using x86 assembly that can search for a word in a text. When the word is present in the text, the program will inform the user. I'm still having a problem in comparing the strings. Any advice?
.model small
.stack 200h
.data
message1 db "Enter your text here: $"
text db 150,151 dup(0)
message2 db 10,13,"Enter the word that you want to find: $"
find db 20,21 dup(0)
yesmessage db 10,13,"The word is in the text$"
nomessage db 10,13,"Sorry the word is not in the text$"
.code
Start:
;Display message and key in strings
mov ax,seg message1
mov ds,ax
mov si,offset text
mov di,offset find
mov dx,offset message1
mov ah,09h
int 21h
mov dx,si
mov ah,0Ah
int 21h
mov ax,seg message2
mov ds,ax
mov dx,offset message2
mov ah,09h
int 21h
mov dx,di
mov ah,0Ah
int 21h
;compare strings
mov bx,00
mov bl,text+1
mov bh,find+1
cmp bl,bh
jne L1
add si,2
add di,2
L2:mov bl,byte ptr[si]
cmp byte ptr[di],bl
jne L1
inc si
inc di
cmp byte ptr[di],"$"
jne L2
mov ah,09h
mov dx,offset yesmessage
int 21h
L1:mov ah,09h
mov dx,offset nomessage
int 21H
mov ax,4c00h
int 21h
end start
the expected result should be:
Example 1:
Enter your text here: He is old
Enter the word that you want to find: old
The word is in the text
Example 2:
Enter your text here: He is old
Enter the word that you want to find: young
Sorry the word is not in the text
I can see a couple of obvious problems in your code. See my comments below:
mov bx,00 ; this instruction is redundant
mov bl,text+1
mov bh,find+1
cmp bl,bh
jne L1 ; it's quite likely that the strings won't have the same length,
; i.e. that find will be shorter than text. this condition is
; therefore incorrect. it would make more sense to use jl, i.e.
; jumping to the negative print if text is shorter than find.
mov ah,09h
mov dx,offset yesmessage
int 21h
L1:mov ah,09h
mov dx,offset nomessage ; you'll be printing both messages in cases where
int 21H ; the substring is found, because you don't have
; any jump that skips past it.

Resources