Thread: Storing String (Texts) :: ASM

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Storing String (Texts) :: ASM

    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

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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/)

    Code:
    .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....

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM