Hi There,

I'm trying to learn a bit of assembler and have got the grasp of the majority of the basics. However I'm struggling to allow a user to enter a string, terminated by a null character when the user hits carriage return. I'd also like to be able to print the string back out afterwards. I'm using NASM. I've got the following code so far:

.bss

Stringbuff resb 0100H

Sub Routines:

GetString:
	push AX			; Store value of AX
	push DX			; Store value of DX
	mov DI, Stringbuff	; Set start address of StringBuff Array
	cld
DoGet:
	call Getinput		; Gets 1 char input and places in DL
	call Printchar		; Prints character in DL (echos input as you type)
	cmp AL, 0DH		; Check to see if input is a carriage return
	je End			; if it is then end the inputting
	stosb			; else store the character in the array
	jmp DoGet		; Loop

End:
	mov AL, 000H		; Move null termination value into AL
	stosb			; Add null termination to the end of the array
	pop DX			; restore DX
	pop AX  		; Restore AX
	ret	

;********************************************
PutString:
	push AX			; Store value of AX
	push DX			; Store value of DX
	MOV SI, Stringbuff       ; Move offset of stringbuff into SI
DoPrintString:
	lodsb                         ; Load the character from string array
	cmp AL, 0H                 ; Check to see if we've hit the null termination character       
	jle Finish                     ; If we have, finish
        mov DL,AL                  ; Else: Move character into  DL ready to print
	call Printchar		; Prints character in DL
	jmp DoPrintString         ; Start reading loop again
Finish:
	pop DX			; restore DX
	pop AX  		; Restore AX
	ret

Main:

call GetString
call Putsring

The problem:
Well my program seems to allow me to enter the string ok, but then it doesnt print anything back out. If i remove the following lines from the putstring sub it sucessfully prints only the first character i input. But with the following lines in it prints none of the input

cmp AL, 0H                 ; Check to see if we've hit the null termination character       
jle Finish                     ; If we have, finish
jmp DoPrintString         ; Start reading loop again

Well flux123 i dont pretend to be an expert in this assembly programming but have some tricks of my own
1. its better to use TASM cuz it excecutes both the styles of programming.

2. instead of pushing the string into the stack its better to store it in a variable

the program code is below , i have excecuted in tasm n it works perfectly fine

.model small
.stack
.data
        str   db  50 dup('$')   ; for storing string
        msg db 10,13,"enter the string : $"
        dis   db 10,13,"The string entered is :$"

.code
       Mov ax , @data
       Mov ds , ax
       LEA dx , msg
       mov ah , 09h
      int 21h
       XOR cx , cx
       LEA si , str


LP:  Mov ah , 01h
       int 21h
       cmp al , 0dh ; this stops as soon as you press enter 
       JZ stop
       Mov [si] , al
       inc si
       inc cx
       jmp LP

STOP:
         LEA dx , dis
         Mov ah , 09h
         int 21h
         LEA si , str

DISP:
        Mov dl , [si]
        Mov ah , 02h
        int 21h
        inc si
        DEC cx
        JNZ DISP
        Mov ah , 4ch
        int 21h

END

Well try this one and let me know if there any error exist

Hello
how to find the length of a string so that it returns the length of the string in AL (up to 255 characters)assambaly

well when ur accepting the string u could use cx or some register and keep incrementing it aslong as the user doesnt press enter .

Hello, there was no problem I could see in your code.
To others, there are multiple ways to get the length of
a string entered by a user under DOS, for 21/0A
DOS always reads one less than count so there is always
a carriage return at the end, for 21/3F the maximum number
of characters is the amount requested and returns with AX
containing the actual number of characters read. When
reading a character one at a time always null terminate and
you can count to the end in order to get the length.

mov si, str
getlen:
lodsb
cmp al, 0
jnz getlen
dec si
xchg cx, si
sub cx, str
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.