PDA

View Full Version : Storing String (Texts) :: ASM



kuphryn
09-15-2002, 03:27 PM
Hi.

How do you store a string of characters in a variable or register?

-----
...
.DATA

// This line works.
MyString DB 'testing $'

// This line does not work.
// MASM error is: error A2084: constant value too large

MyString DB ?

.CODE

mov MyString, 'testing 1 2 3'
-----

I would like to know how to move a string of characters to a variable and/or a register at any point of the code instead of just in .DATA.

Thanks,
Kuphryn

Fordy
09-15-2002, 05:11 PM
Here's some code for MASM...(though its set for DOS and you will need the old 16 bit linker to get it to work - that's available at http://spiff.tripnet.se/~iczelion/)



.286
.model small

MYSTACK SEGMENT STACK
db 512 DUP('?')
MYSTACK ENDS

MYDATA SEGMENT
source db "Hello World",0
string db 50 DUP(0)
MYDATA ENDS

;################################################# ####
;################################################# ####



.CODE
start:

;
; 1 - Sort out the segemnts
;

mov ax,MYDATA ;get data segment address
mov es,ax ;mov into ES...
mov ds,ax ;...and DS

;
;2 - Get string length
;

lea di, source ;load address of string
xor ax,ax ;ax = 0
mov cx,0FFFFh ;cx = max str len
repnz scasb ;scan for NULL
neg cx ;cx = -len..so make pos
dec cx ;dec for 2s comp
push cx ;save on stack

;
;3 - Copy data
;

cld ;clear direction flag
lea si,source ;load source
lea di,string ;load destination
rep movsb ;copy cx amount of bytes

;
;4 - Print to screen
;

pop cx ;restore cx
lea dx,string ;reload string in dx
mov ah,40h ;print to screen
mov bx,1 ;output handle 1
int 21h ;call dos

;
;5 - Exit prog
;

mov ax,4C00h ;dos exit
int 21h ;call dos


end start

Get used to storing data in segments....the other option you have is building stack frames....and if you dont know what you are doing its difficult....

kuphryn
09-15-2002, 05:21 PM
Okay. Thanks.

Your solution is a bit more than what I know right now. I will be able to understand your solution within the next three weeks.

Kuphryn