Assembly Language: Completing the program to get the midstring - string

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

Related

Printing inputted string in assembly language

I wanna print what the user inputted first. However, it displays the converted uppercase already before the result.
Here's my code:
org 100h
BEGIN:
LEA DX, DASH1
MOV AH, 9
INT 21H
LEA DX,LOWERCASE
MOV AH,09H
INT 21H
LEA SI,STR1
MOV AH,01H
READ:
INT 21H
MOV BL,AL
CMP AL,0DH
JE DISPLAY
XOR AL,20H
MOV [SI],AL
INC SI
JMP READ
DISPLAY:
MOV AL,"$"
MOV [SI],AL
LEA DX,UPPERCASE
MOV AH,09H
INT 21H
LEA DX, STR1
MOV AH, 09H
INT 21H
MOV DL, BH ; space
MOV AH, 02H
INT 21H
LEA DX, UPPERCASE_
MOV AH, 9
INT 21H
LEA DX,STR1
MOV AH,09H
INT 21H
LEA DX, DASH2
MOV AH, 9
INT 21H
MOV AH,4CH
INT 21H
ret
LOWERCASE DB 0DH,0AH,0AH, " Input a 5-letter string: $"
UPPERCASE DB 0DH,0AH, " The uppercase equivalent of $"
UPPERCASE_ DB "is $"
DASH1 DB 0AH,0DH, " ================================================================= $"
DASH2 DB 0AH,0AH,0DH, " ================================================================= $"
STR1 DB 255 DUP(?)
Output:
Input a 5-letter string: asdfg
The uppercase equivalent of ASDFG is ASDFG
^ i want this part to print as `asdfg`
making it The uppercase equivalent of asdfg is ASDFG
P.S. also, how do I make the string limited to 5 only? Its because the string a user can input can exceed more than 5. Any tips?
==============================================================================
You are displaying strings UPPERCASE, STR1, UPPERCASE_,STR1, respectively, no wonder that STR1 remains the same. You need to reserve one more string STRorig where you should store the obtained character before its case is changed.
You can use DI to address STRorig and then perform STOSB for saving original AL to ES:DI and incrementing DI.
When you want to limit the size of input string, compare SI with STR1 + 5 and if it's above, act as if AL was equal to 0DH.

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

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.

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

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