News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Copy from eax to string

Started by Sergiu FUNIERU, March 05, 2010, 04:52:28 AM

Previous topic - Next topic

Sergiu FUNIERU

I have the value 7 in eax, and I want to copy it in the string str1.

I tried this:
.data
str1 db 5 dup (0)

.code
start:
main proc
mov ecx, 0
mov str1[ecx], [eax]
exit
main endp
         end start

but I received this error message:
iindex.asm(22) : Error A2055: Invalid instruction operands

How should I do this properly? I was thinking that str1 is the address of the beginning of the string, so putting 7 at that address will copy the value from eax.

joemc

#1
Quote from: Sergiu Funieru on March 05, 2010, 04:52:28 AM
I have the value 7 in eax, and I want to copy it in the string str1.
makes sense...  but could mean you want 0x07 to be the first character or 0x37 ('7')to be the first character...
to get 0x07:

mov edi, OFFSET str1
mov [edi],eax



and to get the '7' a quick look at str$ in macros.asm suggests:

invoke dwtoa,eax,ADDR str1



Quote from: Sergiu Funieru on March 05, 2010, 04:52:28 AM
so putting 7 at that address will copy the value from eax.
really not sure what you mean


all together:

include \masm32\include\masm32rt.inc
.data
jstring db 0,0,0,0,0

.code
start:

mov eax, 7 ;
mov edi, OFFSET jstring
mov [edi],eax
print edi
print "Printed 0x07 which Beeps!",13,10

mov eax, 7 ;
mov edi, OFFSET jstring
invoke dwtoa,eax,edi
print edi
print chr$(13,10)
print "Printed 0x37 which says 7",13,10

inkey
exit

end start


is this for your 100 to 1 Compressor?  :bg

jj2007

Here is a sample of what are valid operations. Have a look at it through OllyDbg...

include \masm32\include\masm32rt.inc

.data
str1 db 5 dup (0)

.code
start:
nop
main proc
mov ecx, 0
;mov str1[ecx], [eax] fails
mov str1, al
mov str1, ah
mov word ptr str1, ax
mov dword ptr str1, eax
mov edx, offset str1
mov [edx+ecx], al
mov [edx+ecx], ah
mov [edx+ecx], ax
mov [edx+ecx], eax

exit
main endp
nop
end start

BogdanOntanu

Quote from: Sergiu Funieru on March 05, 2010, 04:52:28 AM
I have the value 7 in eax, and I want to copy it in the string str1.

That is bad conceptual thinking.

EAX contains a number (7) and you want to move "a number" into "a string"? They are of diferent types, of diferent sizes and of diferent representations ... hence this is not conceptually possible. You are lucky because the assembler gives you an error based on the different sizes: EAX is 4 bytes and your string is made of multiple 1 byte elements.

Usual "strings" contain an ASCII  representation of the number's digits and a null (zero byte) terminator. 

Hence You must convert the value contained in EAX into a string.

The "dwtoa" procedure from MASM32 can help you perform this conversion... or alternatively you can write your own procedure in order to learn how to convert a number value into an ASCII string.

Then you should note that there are other string conventions like Unicode or Pascall strings.

Then you should note that this conversion depends on the base of conversion: hexadecimal, decimal, octal, binary.

Then you should drop the bad habbit of optimizations especially when learning.

For example you reserve 5 bytes for your result string. However a 32 bits number value in EAX register -- when converted to a string -- could have as much as 8 digits in hexadecimal and more in decimal or binary representations. In connsequence you expose yourself to errors for no reason at all.

Leave enough space for your strings and variables and forget about optimizations for a while ;)

If your "teacher" told you that you should optimize everything no matter what THE he/she was a very bad teacher. Optimization is "the root of all evil" and it makes you loose fiability, flexibility, scalability and ease of understanding... in order to gain speed or space ...  and you do not want this now.

Oh... and I see that you are still trying to move from memory: "str [ecx]" to memory "[eax]". This is another conceptual error by itself.

In ASM you can rarely move from memory to memory. Normally you move form memory into a register and then from a register back to memory. It is a two stage operation that you must get used to.

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro