Question:
How to copy one string in another in assembly language programming?
2014-06-18 01:11:25 UTC
When I code the following code , there is no error , but I do not see any output. Please add additional lines so that I can see the output. (I use MASM in windows xp). The code is as follows:

.MODEL SMALL
.STACK 100H
.DATA
STR1 DB 'BANGLADESH$'
STR2 DB ?

.CODE
MAIN PROC

MOV AX,@DATA
MOV DS,AX ;inyializing segment register
MOV ES,AX

MOV CX,0H ;intializing counter
LEA SI,STR1

LEVEL:
MOV AL,[SI]
CMP AL,'$'
JE LEVEL1 ;calculating length
INC CX
INC SI
JMP LEVEL

LEVEL1:
CLD ;clear DF
LEA SI,STR1 ;SI points str1
LEA DI,STR2 ;DI points str2

LEVEL2: ;copy string
MOVSB
LOOP LEVEL2


MOV AH,4CH ;dos exit
INT 21H

MAIN ENDP
END MAIN


Please let me know by adding additional lines in the program so that I can see the output.
Three answers:
2014-06-19 06:43:57 UTC
There are a few problems with your code.



The first problem is that there is no code to print the text. (You are using the '$' to mark the end of the string, so I'm assuming that you are using DOS function 9 for string printing.)



The second problem is that you are not allocating the proper space for STR2. You only allocated ONE BYTE and your code is trying to copy the 11 bytes from STR1 to STR2's memory address. To allocate a block of data, you can use the 'dup' assember instruction.

Examples:

STR2 db 1024 dup (?) ;allocate 1024 uninitialized bytes

STR2 dw 400 dup (?) ;allocate 400 uninitialized words

STR2 db 300 dup ('#') ;allocate 300 bytes, with the bytes set to the '#' ASCII number

STR2 dw 120 dup (0) ;allocate 120 words, with the bytes set to the value of 0



The third problem is that you are using 2 loops when you only need 1 loop to copy a string and print it. Your first loop just counts the bytes in STR1, using CX as the counter. Then the CX value is used as the

counter for the movsb loop. All you need is 1 loop to copy the string before you use DOS function 9 for printing.



I present my code (using your coding methods), but without the second loop:

;NOTE: The code below can be optimized further to make it run faster.



.model small

.stack 100h

.data

STR1 db "BANGLADESH$"

STR2 db 11 dup (0)

.code

;NOTE: You do not need to create a 'MAIN' procedure!!!!

mov ax, @data ;set DS to our data segment

mov ds,ax

mov si,offset STR1 ;I like using "mov [reg], offset [label]" method instead of LEA

mov dx,offset STR2 ;DS:DX holds address of STR2



myloop:

mov al,[si] ;AL = byte at DS:SI

cmp al, '$' ;test for end of source string

je printit ;jump if end of string reached

mov [dx],al ;copy byte to destination string STR2

inc si ;increment STR1 and STR2 pointers

inc dx

jmp myloop



printit:

mov [dx],al ;copy the '$' to end of STR2

mov dx,offset STR2 ;DS:DX = starting address of STR2

;NOTE: DOS function 9 prints a string that is terminated with '$'

; DS:DX points to the string.

mov ah,9

int 21h

mov ax,4c00h ;exit to DOS and return 0

int 21h

end
Techwing
2014-06-18 01:44:06 UTC
You don't include any code to display the output, you just exist from the program. Assembly language is a whole different ball game from higher-level languages.
2014-06-18 07:57:31 UTC
Dear Techwing, I know that function 9 is used to display the output. Can you please write down where I should add the code ?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...