hi i just wonder how to limit an input when a user types a certain number.

for example:

Input: 5
Input: abacd
Output: dcaba

here my problem is when a user inputs 5, then the allowed number for inputting characters is 5 only. and then it reverses the string.
I only have the code for reversing the string but not the limiting of #s to input.

I had an idea to fix this but it failed

i hope you would help me fix this one. thanks

.model small
.stack 100h
.data
	m1 db 13, 10, "Enter string: $"
	m2 db 13, 10, "Reverse string is: $"

.code
	mov ax, @data
	mov ds, ax
	lea dx, m1
	mov ah, 09h
	int 21h
	mov cx, 0

read:
	mov ah, 01
	int 21h
	cmp al, 0dh
	je ahead
	push ax
	inc cx
	jmp read

ahead:
	lea dx, m2
	mov ah, 09h
	int 21h

display1:
	mov ah, 02h
	pop dx
	int 21h
	loop display1

	mov ah, 4ch
	int 21h
end

The code is already counting chars with CX, so you can jump out of that loop when CX hits 4.
You can also replace that Int21h/01h with Int21h/0ah (Buffered STDIN Input) where DX is the address of your input buffer and the first byte of that buffer is the maximum bytes to read and the second byte is the number of bytes read ao far.

.model small
.stack 100h
.data
        mm db 13, 10, "Enter a digit: $"
	m1 db 13, 10, "Enter string: $"
	m2 db 13, 10, "Reverse string is: $"

.code
	mov ax, @data
	mov ds, ax
	lea dx, m1
	mov ah, 09h
	int 21h
	mov cx, 0

read:
	mov ah, 01
	int 21h
	cmp al, 0dh
	je ahead
	push ax
	inc cx
	jmp read

ahead:
	lea dx, m2
	mov ah, 09h
	int 21h

display1:
	mov ah, 02h
	pop dx
	int 21h
	loop display1

	mov ah, 4ch
	int 21h
end

how to use the BYTE???

How do you use it for what, and where? the BYTE marker is meant to tell the assembler that you're working with a 1-byte value; you use it to remove ambiguity in places where the value could be either a byte or a word. I don't really see anything like that in your code.

BTW, you have added the string to ask for the size of the string, but you still don't print it out, or read in the size value.

thanks. after i put the printing code, what should i do to get the number of the byte that the program should display? for example, if i input a digit: 3 then the string should limit only to 3 characters and then reverse the output..

thanks. after i put the printing code, what should i do to get the number of the byte that the program should display?

If you mean to take a single digit(i.e. the user will be able to enter at most nine characters only).
then here's how :

get_limit:
	mov ah,01h	;get the input
	int 21h
	xor ah,ah	;ah = 0
	sub al,48d	;convert to decimal from ascii
	mov cx,ax	;cx=the digit you entered
	mov si,ax	;also copy it to si(used afterwards)

Even if you want to handle "multidigit" number, you can use this as a base code.It would be a good exercise.

Now print your prompt and read the characters:

;print second message
	mov dx,offset m1;copy the address to dx
	mov ah,09h      ;display
	int 21h

read_string:
	mov ah,01	;take input
	int 21h
	push ax		;save it in the stack
	dec cx		;decrease counter
	cmp cx,0        ;if limit reached
	je write_string	;jump to write_string
	jmp read_string	;loop

And then finally print it in reverse.
In the code you wrote above-

display1:
mov ah, 02h
pop dx
int 21h
loop display1
 
mov ah, 4ch
int 21h

you have not mentioned when to stop,so the program will go on printing and probably crashes thereafter.
Here's a modified version of that.

write_string:
	mov dx,offset msg3	;print the third message
	mov ah,09h
	int 21h
	

display1: 
	pop dx		;finally display the string in reverse
	mov ah,02h		
	int 21h
	dec si		;decrease the counter(cx was copied to si,remember!)
	cmp si,0        ;finished printing? 
	je stop	        ;then exit	
	loop display1   ;otherwise loop again

Hope this helps.

Edited 5 Years Ago by D33wakar: n/a

hi diwakar wagle! thanks for your reply it helped me a lot. How can i allow for multidigit?

Now you're gonna have to show some effort here.

How can i allow for multidigit?

You can do it youself if you have understood my code.
hints:
-> declare an extra word variable
-> take input, char by char and save it in the new word variable.
-> use it as counter, like my example code.

that extra word variable will i declare in the data segment?
and when i take input, does that mean am gonna use 01h?

that extra word variable will i declare in the data segment?

yep.Though it can be declared in code segment also but we rather keep things simple here.

when i take input, does that mean am gonna use 01h?

yeah that's right. I said "char by char"(means one character at a time) remember!

Edited 5 Years Ago by D33wakar: n/a

hi i think i got the wrong logic

pls. see my code below:

.DATA
	VAR WORD DUP(?)
    mm db 13, 10, "Enter a digit: $"
	m1 db 13, 10, "Enter string: $"
	m2 db 13, 10, "Reverse string is: $"

hi i think i got the wrong logic

pls. see my code below:

.DATA
	VAR WORD DUP(?)
    mm db 13, 10, "Enter a digit: $"
	m1 db 13, 10, "Enter string: $"
	m2 db 13, 10, "Reverse string is: $"

I only said to declare a word variable

var dw ?

not an array of words

VAR WORD DUP?

Take a look at this.

Edited 5 Years Ago by D33wakar: n/a

hi thanks for the link, but when i tried var dw ? it had errors

Edited 5 Years Ago by bettybarnes: n/a

Could you tell us what error messages you get, please? If I understand correctly, the syntax for defining a single uninitialized word variable in Turbo Assembler is

var dw ?

What exactly is going wrong with it as given?

This article has been dead for over six months. Start a new discussion instead.