Get String Then Read to Display Assembly
#1
Read in a cord and Brandish it
Posted 12 April 2013 - 01:00 PM
I am trying to write a program in assembly language that prompts the user to enter in a string of words. And so prompt the user to enter in a word from the original line of text entered by the user. Then prompt the user to enter in a word to supersede that showtime word of equal length. Afterward each prompt the user will enter in the info and press enter. After all the info is entered it the programme is supposed to brandish the original line the user entered and so on the next line the same line equally earlier but with every instance of the kickoff unmarried discussion entered replaced by the 2nd single word entered. I am having difficulty with displaying the original cord of words. I am not sure why but every time it outputs "p". I accept not begun piece of work on the code for replacing the give-and-take with the other word. Any help would exist appreciated.
MODEL small .STACK 100h WriteSt MACRO MessLab button ax push button dx mov ah,9 mov dx,offset MessLab int 21h pop dx pop ax ENDM .DATA prompt1 db 10,xiii,'Enter Original Line:$' prompt2 db 10,xiii,'Enter Showtime Give-and-take: $' prompt3 db 10,13,'Enter Replacement Give-and-take: $' symbol db '$' first db ? replacement db ? crlf db x,13,'$' string db 80 .CODE MAIN PROC mov ax,@Information mov ds,ax WriteSt prompt1 ;brandish prompt1 WriteSt crlf lea DI,string Call ReadString ;get string WriteSt crlf WriteSt prompt2 ;display prompt2 lea DI,first Phone call ReadString ;get start WriteSt prompt3 ;display prompt3 lea DI,Replacement Call ReadString ;become replacement WriteSt crlf WriteSt crlf lea SI, cord mov bx,80 CALL DisplayString ;brandish original string mov ax,4C00h int 21h MAIN ENDP ReadString PROC Virtually ; Reads and stores a cord. ; input: DI offset of string ; output: DI commencement of string ; BX number of characters read PUSH AX Button DI CLD ;procedure from left XOR BX,BX ;no. of chars read MOV AH,1 ;input char role INT 21H ;read a char into AL WHILE1: CMP AL,0DH ;<CR>? JE END_WHILE1 ;yep, exit ;if char is backspace CMP AL,8H ;backspace? JNE ELSE1 ;no, store in string ;then DEC DI ;yes, movement string ptr back DEC BX ;decrement char counter JMP READ ;and get to read another char ELSE1: STOSB ;store char in cord INC BX ;increase char count READ: INT 21H ;read a char into AL JMP WHILE1 ;and continue loop END_WHILE1: POP DI POP AX RET ReadString ENDP DisplayString PROC ;displays a string ;input: SI = offset of string ; BX = no. of chars. to brandish ;output: none Push AX Button BX Button CX PUSH DX PUSH SI MOV CX,BX ;no. of chars JCXZ P_EXIT ;exit if none CLD ;process left to right MOV AH,two ;prepare to print Acme: LODSB ;char in AL MOV DL,AL ;motion it to DL INT 21H ;impress char LOOP Peak ;loop until done P_EXIT: Pop SI Pop DX Popular CX POP BX POP AX RET DisplayString ENDP cease MAIN
#two
Re: Read in a string and Display it
Posted 13 April 2013 - 10:21 AM
Ok, let's alter a few things.
This:
first db ?
Holds but i grapheme, lets change it to:
first db 80 dup(?)
At present it (the buffer) volition concur 79 characters.
Aforementioned for replacement db ?, should exist:
replacement db 80 dup(?)
This:
cord db 80
is a string with the character "P", 80 is the ASCII code for capital P. Change that to:
cord db eighty dup(?)
Change your ReadString to:
ReadString PROC Near ; si == arrow to buffer Read: mov ah, 01H int 21H ; read ane character cmp al, 13 ; is information technology return? je Done ; yep, we are done mov [si], al ; no, move charater into buffer inc si ; increase arrow jmp Read ; loop back Done: mov [si], "$" ; NULL Terminate string ret ReadString ENDP
.MODEL pocket-sized .STACK 100h WriteSt MACRO MessLab push ax button dx mov ah,9 mov dx,offset MessLab int 21h pop dx pop ax ENDM .DATA prompt1 db 10,13,'Enter Original Line:$' prompt2 db 10,13,'Enter Commencement Word: $' prompt3 db x,13,'Enter Replacement Word: $' symbol db '$' first db eighty dup(?) replacement db fourscore dup(?) crlf db 10,13,'$' cord db eighty dup(?) .Lawmaking MAIN PROC mov ax,@DATA mov ds,ax WriteSt prompt1 ;brandish prompt1 WriteSt crlf mov si, beginning string Telephone call ReadString ;get string WriteSt string WriteSt prompt2 ;display prompt2 mov si, starting time get-go Call ReadString ;get first WriteSt prompt3 ;brandish prompt3 mov si, first replacement Phone call ReadString ;go replacement WriteSt string ; lea SI, string ; mov bx,80 ; Call DisplayString ;display original cord mov ax,4C00h int 21h Master ENDP ReadString PROC NEAR ; si == arrow to buffer Read: mov ah, 01H int 21H ; read i character cmp al, 13 ; is information technology render? je Done ; yes, we are washed mov [si], al ; no, move charater into buffer inc si ; increment pointer jmp Read ; loop back Done: mov [si], "$" ; Cypher Terminate cord ret ReadString ENDP
At present, work on the lawmaking to replace characters/words. You take three buffers, 1 contains original string, 2 contains word to replace, and tertiary contains replacement word.
#3
Re: Read in a string and Display it
Posted 16 April 2013 - 04:13 PM
Thanks for the help. I am at present having an issue with the finding and replacing of a word in a string. It doesn't replace the discussion in the string. Also it but prints out a agglomeration of garbage. Can I get some help over again?
MODEL small .STACK 100h WriteSt MACRO MessLab push ax push dx mov ah,9 mov dx,first MessLab int 21h pop dx pop ax ENDM .Data prompt1 db ten,thirteen,'Enter Original Line:$' prompt2 db 10,13,'Enter First Word: $' prompt3 db 10,thirteen,'Enter Replacement Give-and-take: $' symbol db '$' first db 80 dup(?) replacement db 80 dup(?) crlf db 10,13,'$' string db fourscore dup(?) .CODE MAIN PROC mov ax,@Data mov ds,ax WriteSt prompt1 ;Brandish prompt1 WriteSt crlf ;New line lea SI,cord ;Load cord into SI Telephone call ReadString ;Get info from keyboard WriteSt crlf ;New line WriteSt prompt2 ;Display prompt2 lea SI,first ;Load first into SI Telephone call ReadString ;Get info from keyboard WriteSt prompt3 ;Display prompt3 lea SI,replacement ;Load replacement into SI Call ReadString ;Get info from keyboard WriteSt crlf ;New line WriteSt crlf ;New line lea SI, string ;Load string into SI mov bx,lxxx ;Number of characters to be displayed Call DisplayString ;Brandish string CLD lea DI, string mov al,commencement Top2: REPNE SCASB JE Replace Replace: mov al,replacement STOSB cmp cx,0 JNE Top2 mov cx,dx lea SI,string Repeat1: LODSB mov dl,al mov ah,2 int 21h Loop Repeat1 lea SI, string mov bx,80 Call DisplayString mov ax,4C00h ;Leave to dos int 21h MAIN ENDP ReadString PROC NEAR Read: mov ah,01h int 21h ;Read 1 character cmp al,13 ;Is it render? je Washed ;Yes, we are washed reading in characters mov [si],al ;No, move grapheme into buffer inc si ;Increment pointer jmp Read ;loop back Washed: ret Readstring ENDP DisplayString PROC ;displays a string ;input: SI = offset of string ; BX = no. of chars. to brandish ;output: none Button AX PUSH BX PUSH CX PUSH DX Push SI MOV CX,BX ;no. of chars JCXZ P_EXIT ;exit if none CLD ;process left to right MOV AH,2 ;set to print TOP: LODSB ;char in AL MOV DL,AL ;movement it to DL INT 21H ;print char LOOP Top ;loop until done P_EXIT: POP SI Pop DX POP CX Pop BX Popular AX RET DisplayString ENDP end MAIN
#four
Re: Read in a cord and Display it
Posted sixteen April 2013 - 06:42 PM
Quote
it just prints out a bunch of garbage
Why wouldn't it? Your strings are not "Aught" terminated. In DOS, strings are terminated with a dollar sign - $. Wait at the code I gave you:
ReadString PROC NEAR ; si == arrow to buffer Read: mov ah, 01H int 21H ; read 1 grapheme cmp al, xiii ; is it render? je Done ; yes, we are done mov [si], al ; no, move charater into buffer inc si ; increase arrow jmp Read ; loop back Done: mov [si], "$" ; NULL End cord ret ReadString ENDP
Now, look at the code you posted:
ReadString PROC Almost Read: mov ah,01h int 21h ;Read one grapheme cmp al,thirteen ;Is it return? je Washed ;Yeah, nosotros are washed reading in characters mov [si],al ;No, move graphic symbol into buffer inc si ;Increase arrow jmp Read ;loop back Washed: ret Readstring ENDP
Detect something missing in your version of Readstring?
Also, as a parameter to DisplayString, you are passing 80 for the corporeality of characters to impress, are you sure you are going to print eighty characters each time? Nosotros can modify ReadString to render the amount of characters read:
ReadString PROC Nigh mov cx, si ; cx == buffer start address Read: mov ah,01h int 21h ;Read 1 character cmp al,xiii ;Is it return? je Done ;Yeah, nosotros are washed reading in characters mov [si],al ;No, move character into buffer inc si ;Increase arrow jmp Read ;loop back Done: mov [si], "$" ; terminate string mov ax, si ; ax == buffer end accost sub ax, cx ; return characters read in ax ret Readstring ENDP
and to utilise, create a variable to concord each buffers length. For the first buffer - string it volition be defined as: string_len dw ? or whatever you want to call it. Then afterward to make full the string buffer, move ax into this variable:
lea SI,string ;Load string into SI Telephone call ReadString ;Get info from keyboard mov string_len, ax
now string_len contains the count of characters put into the buffer including the terminating $
Also, to replace text, the easiest way is to have ii buffers - the original string (source) and a buffer to concur the modified string (destination). What happens if I enter Hello Mr. Gunner as the original text, the word to supplant Mr. and the replacement word mister? You are going to overwrite something, so this is where a destination buffer will help, you only have to make certain the dest buffer is large enough to agree the modified string.
What you would do is:
Take your string and loop through information technology byte by byte moving each byte into the dest buffer, until yous get to the get-go letter of the word to supervene upon, relieve that address, now since this letter can appear many times in the string and it might not be the replacement word, compare each byte after the first one to the replacement word, if information technology matches, add the replacement word to the accost you lot saved and continue moving the rest of the cord into the dest buffer.
#5
Re: Read in a string and Brandish it
Posted 17 April 2013 - 02:01 PM
Quote
Take your string and loop through it byte by byte moving each byte into the dest buffer, until you go to the first letter of the word to supercede, save that address, now since this letter tin announced many times in the string and it might not be the replacement word, compare each byte after the first one to the replacement word, if information technology matches, add the replacement discussion to the accost you saved and go on moving the rest of the cord into the dest buffer.
Am I getting shut at all to accomplishing this with this lawmaking? I am non skilful at assembly language obviously and am trying to teach myself. I know the thought of what I need to do but tin't effigy out how. I was able to do this in c++, simply I tin't seem to get it for assembly.
CLD lea DI, string mov al,first Top2: REPNE SCASB JE Replace Replace: mov al,replacement STOSB cmp cx,0 JNE Top2
#6
Re: Read in a string and Display it
Posted 17 April 2013 - 03:22 PM
Quote
You are doing this on your ain? Not for school or any type of course? If it is for you lot, then I will recommend you to acquire 32bit Assembly instead of 16bit DOS code.
Quote
Am I getting close at all to accomplishing this with this code?
You tell me, what happens when you run it?
First problem I encounter is you move a character from first into al for repne scasb, so you do your replacement and so jump back to Top2 and repeat repne scasb with whatsoever was in al from stosb afterwards your replacement, you need to get the next character from first. Also, where practice you load a value into cx? Yous test information technology for 0, simply it could be 0 to begin with.
The easiest manner to see if it works, is to run the code, come across what happens, modify some code and run again.
#7
Re: Read in a cord and Brandish it
Posted 18 April 2013 - 01:12 PM
I am doing this for a class but my teacher didn't give any instruction for this, so I am instruction myself how to do it.
I am getting an error when I run the program. It happens every bit soon every bit I hit enter later entering in the replacement word.
the NTVDM CPU has encountered an illegal instruction.
CS:05ac IP:0080 OP:ff ff 00 a0 98
This error only happens when I enter the following data (original line=this is the line i am entering from the keyboard.) first=the replacement=myy
Merely when I use different word for replacement such as "at present" information technology runs through the plan.
When I employ "at present" for the replacement it gives me this output.
Enter Original line:
This is the line I am entering from the keyboard.
Enter First Discussion: the
Enter Replacement Word: now
This is the line I am entering from the keyboard.
this is the line I am entering from the keyboard.
I am still having difficulty with the replacement role. I fabricated cx equal the length of beginning. I changed DI to SI on line 60 and then incremented information technology on line 71. Which is what I believe is how to movement to the next character in string, but I am thinking I am wrong. I don't know what I am supposed to change.
MODEL small .STACK 100h WriteSt MACRO MessLab button ax button dx mov ah,9 mov dx,showtime MessLab int 21h pop dx pop ax ENDM .DATA prompt1 db x,13,'Enter Original Line:$' prompt2 db x,13,'Enter First Give-and-take: $' prompt3 db x,13,'Enter Replacement Word: $' symbol db '$' get-go db 80 dup(?) replacement db 80 dup(?) crlf db x,13,'$' string db 80 dup(?) string_len dw ? string_len1 dw ? .CODE MAIN PROC mov ax,@DATA mov ds,ax WriteSt prompt1 ;Brandish prompt1 WriteSt crlf ;New line lea SI,cord ;Load cord into SI Phone call ReadString ;Get info from keyboard mov string_len,ax WriteSt crlf ;New line WriteSt prompt2 ;Display prompt2 lea SI,offset ;Load commencement into SI Call ReadString ;Become info from keyboard mov string_len1,ax WriteSt prompt3 ;Display prompt3 lea SI,replacement ;Load replacement into SI Telephone call ReadString ;Get info from keyboard WriteSt crlf ;New line WriteSt crlf ;New line lea SI, cord ;Load string into SI mov bx,string_len ;Number of characters to be displayed Call DisplayString ;Display cord mov cx,string_len1 CLD lea SI, string mov al,first Top2: REPNE SCASB JE Replace Replace: mov al,replacement STOSB cmp cx,0 inc SI JNE Top2 WriteSt crlf lea SI, string mov bx,string_len Telephone call DisplayString mov ax,4C00h ;Get out to dos int 21h Master ENDP ReadString PROC NEAR mov cx,si Read: mov ah,01h int 21h ;Read 1 character cmp al,xiii ;Is information technology return? je Done ;Yes, nosotros are washed reading in characters mov [si],al ;No, move character into buffer inc si ;Increment pointer jmp Read ;loop dorsum Done: mov [si],"$" mov ax,si sub ax,cx ret Readstring ENDP DisplayString PROC ;displays a string ;input: SI = offset of cord ; BX = no. of chars. to brandish ;output: none PUSH AX PUSH BX PUSH CX PUSH DX Push SI MOV CX,BX ;no. of chars JCXZ P_EXIT ;exit if none CLD ;process left to right MOV AH,two ;set up to print Peak: LODSB ;char in AL MOV DL,AL ;move information technology to DL INT 21H ;print char LOOP TOP ;loop until done P_EXIT: POP SI POP DX POP CX POP BX POP AX RET DisplayString ENDP end MAIN
#8
Re: Read in a string and Display it
Posted 18 Apr 2013 - 01:26 PM
give me a few hours. I will be home afterwards and show you an example
#9
Re: Read in a string and Brandish it
Posted nineteen April 2013 - 08:27 AM
Thank you lot for all your help Gunner. Non just in this post merely I've looked at other posts that y'all take been in and they have all helped.
#10
Re: Read in a string and Brandish information technology
Posted 19 Apr 2013 - x:58 PM
Ok, this should become you going on your way. This is simple and crude, and there are a few things:
1. String to supersede and replacement string must be same length (this can be changed, just needs a second buffer and more than code)
2. Information technology simply searches for first letter of first word and replaces from there. (To be proper, needs to notice whole get-go word)
.MODEL small .STACK 100h WriteSt MACRO MessLab push ax push dx mov ah,9 mov dx,offset MessLab int 21h pop dx popular ax ENDM .DATA prompt1 db x,13,'Enter Original Line:$' prompt2 db 10,thirteen,'Enter Starting time Word: $' prompt3 db 10,13,'Enter Replacement Word: $' prompt4 db 10, 13, 'Text not found $' prompt5 db 10, xiii, "Strings are not the same length $" symbol db '$' crlf db ten,13,'$' .data? string_len dw ? repl_len dw ? first_len dw ? offset db 80 dup(?) replacement db lxxx dup(?) string db 80 dup(?) .Code Main PROC mov ax, @DATA mov ds, ax DisplayPrompt: WriteSt prompt1 ;Display prompt1 lea SI, cord ;Load string into SI Call ReadString ;Get info from keyboard mov string_len, ax WriteSt prompt2 ;Brandish prompt2 lea SI, first ;Load offset into SI Call ReadString ;Get info from keyboard mov first_len, ax WriteSt prompt3 ;Display prompt3 lea SI,replacement ;Load replacement into SI Call ReadString ;Become info from keyboard mov repl_len, ax ;##################################### cmp ax, first_len ;# are word to supersede and replacement discussion aforementioned length? je StartFindWord ;# Yes!, continue WriteSt prompt5 ;# NO!!! jmp DisplayPrompt ;# Kickoff over StartFindWord: mov si, offset string ;# Address of entered cord buffer mov bx, offset showtime ;# Accost of word to supplant buffer xor cx, cx ;# zero counter FindWord: mov al, byte ptr [bx] ;# Go first letter of word to replace cmp al, byte ptr [si] ;# Compare with byte at pointer in si je ReplaceStart ;# if equal, word institute ;# otherwise continue search inc si ;# increase our string arrow to get next letter of the alphabet inc cx ;# increase our counter cmp cx, string_len ;# does counter equal string length? jne FindWord ;# No, loop back to check adjacent char jmp NotFound ;# Yeah, word to supersede non establish ReplaceStart: mov bx, offset replacement ;# Address of replacement word xor cx, cx ;# zero counter ReplaceIt: ;# si contains pointer to first char of discussion to supervene upon mov al, byte ptr [bx] ;# get byte of replacement word mov byte ptr [si], al ;# replace char in si inc si ;# increase string pointer inc bx ;# increase replacement arrow inc cx ;# increment loop counter cmp cx, repl_len ;# does information technology equal replacement text len? jne ReplaceIt ;# No, loop back to supervene upon next char WriteSt string ;# display modified string RepDone: mov ax, 4C00h ;# Exit to dos int 21h NotFound: WriteSt prompt4 ;# Text to supercede non found!! jmp RepDone ;##################################### MAIN ENDP ReadString PROC Most mov cx, si ; cx == buffer first address Read: mov ah,01h int 21h ;Read i character cmp al,thirteen ;Is information technology render? je Done ;Yeah, we are done reading in characters mov [si],al ;No, motility character into buffer inc si ;Increase pointer jmp Read ;loop back Done: mov [si], "$" ; stop string mov ax, si ; ax == buffer stop address sub ax, cx ; return characters read in ax ret Readstring ENDP DisplayString PROC ;displays a string ;input: SI = offset of string ; BX = no. of chars. to brandish ;output: none Button AX Push button BX Push button CX Push DX Push SI MOV CX,BX ;no. of chars JCXZ P_EXIT ;get out if none CLD ;process left to right MOV AH,two ;prepare to print TOP: LODSB ;char in AL MOV DL,AL ;move it to DL INT 21H ;print char LOOP Top ;loop until done P_EXIT: POP SI Popular DX Pop CX POP BX Popular AX RET DisplayString ENDP end Main
#11
Re: Read in a string and Display it
Posted 20 April 2013 - 02:05 PM
Thank you. That was so much help. I never would have thought of using pointers. I have never been practiced with them.
Source: https://www.dreamincode.net/forums/topic/318365-read-in-a-string-and-display-it/
Post a Comment for "Get String Then Read to Display Assembly"