Member Avatar for kamyarn

Hi
I want to read a string (Last name) from keyboard, concatenate it to another string (First Name) and then print it on screen. (Windows, EMU8086, EXE Template)

First I declare the variables in data segment

msg1 db 0ah,0dh,"Enter your last name: $" ;prompt message
msg2 db "Kamiar " ;first name var
lastname db 20,20 dup(?) ;srting var to save input

then print prompt message and get input

lea dx,msg1
mov ah,09h
int 21h
call read

read:
lea dx,lastname
mov ah,0ah
int 21h

concatenate it to the first name

add msg2,lastname

finally print it

add msg2,'$' ;add $ to end of string
mov dx,msg2
mov ah,09h
int 21h

but it's not working :(
Can you help me to resolve this?

That add command will not do what you think.

Since there is no dollar sign terminating the first name, just do the
int 21h/09h
with DX set to the address of the first name. It will continue printing until it hits the dollar sign after the last name.

...which means your first name will need a label.

Also, instead of padding the last name with spaces, pad it with dollar signs. :)

[AFTERWARD]
Once that is fixed, make another program with add commands in it and watch what they do in debug.

Member Avatar for kamyarn

Thank you thines01

I've changed the codes to

lea dx,msg1 ;prompt
mov ah,09h
int 21h

mov ah,0ah ;get input
lea dx,lastname
int 21h

lea dx,msg2 ;print firstname part
mov ah,09h
int 21h

lea dx,lastname ;print lastname part
mov ah,09h
int 21h

but it didn't work for lastname, printed some stranger character on screen. do you know why it happened?

If you look at this in debug, is your last name terminated with a dollar sign?
Also, you only need one print.
Print the address of the first name and it will also print the last name until it hits the dollar sign.
My guess is that you did not fill enough dollar signs in the last name.

Member Avatar for kamyarn

Thank you, sorry I'm new to assembly and I've no idea to add $ sign to the last name :S

When add $ sign to string (on prompt) it'll show the concatenation mostly correct. I say mostly because it shows two sqaure between first and last name instead of spaces!

data segment

msg1 db 0ah,0dh,'Enter your family:$'
msg2 db 0ah,0dh,'Kamiar'

lastname db 21,21 dup(?)

code segment:

lea dx,msg1
mov ah,09h
int 21h

lea dx,lastname
mov ah,0ah
int 21h

lea dx,msg2
mov ah,09h
int 21h

Sorry for poor English

lastname db 21,21 dup(?)

Are you sure that this is what you want? Unless memory fails me, db 21, 21 dup(?) is asking the assembler to generate a byte array consisting of 014h, followed by 21 undefined bytes. I think what you want is something more like:

lastname db ' ', 20 dup('$')

You are also passing the address of the first byte of lastname to Int 21h, which will overwrite the space code intended for separating the first and last name. Put the space at the end of the FirstName string instead.

By the way, in your original post, you tried to "ADD" strings, and or characters, using ADD:

add msg2,lastname
  ......or....
      add msg2,'$'

Certainly an interesting concept, but totally foreign to assembly language. ADD is a machine instruction that adds integers. What you were actually doing in the first case was adding the pointer for lastname to the first 2 characters of msg2, then replacing the first 2 characters of msg2 with this rather interesting, but nonsensical, result. In the second case, you were adding the first character in msg2 to the '$' character and then replacing the first character of msg2 with this odd, and probably undefined, character. Concatenating strings is a somewhat more involved process which is frequently provided as a feature in high level languages - often overloaded on the addition operator. I suspect that this is what you were thinking. You need to change your mindset when working in assembler. You can never assume that something is done for you (i.e., using ADD to add strings). Perhaps you should write a function that takes 2 input strings and returns a concatenated version. It would be a good exercise for you.

Member Avatar for kamyarn

Thanks, it's solved :)

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.