The MASM Forum

General => The Campus => Topic started by: Joao Batista on November 08, 2012, 09:54:46 AM

Title: dq arrays usage
Post by: Joao Batista on November 08, 2012, 09:54:46 AM
Hello! i'm working on a compiler for a subject at the university, and i don't know how to use a double array.

This is the code, i HAVE to work with the coprocessor for this (is a constraint we have).
I want to know, how can i get a value from a position, and how to store a value to a certain position.

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.data

HelloWorld db "Hello World!", 0
msgOK db "Es igual",0
msgNAK db "No es igual",0

array dq 5 dup(1.5)
i dq 8
result dq 1.5
pos dq 0
aux dd 0
avalue dq 0

.code
start:

MOV aux, OFFSET array
FLD aux
FADD i
FSTP pos
FLD qword ptr [pos]
FSTP avalue
FLD avalue

FLD result
FCOM
fstsw ax
sahf
JE PRINTOK
JMP PRINTNAK

PRINTOK:
    invoke MessageBox, NULL, OFFSET msgOK, OFFSET HelloWorld, MB_OK
    JMP ENDPROGRAM

PRINTNAK:
    invoke MessageBox, NULL, addr msgNAK, addr HelloWorld, MB_OK
    JMP ENDPROGRAM

ENDPROGRAM:
invoke ExitProcess, 0

end start


This code compiles, but when i see if i the value i get is equal to my "result" value, altough i initialize them with 1.5, they aren't the same. Help please!!

And sorry for my english, is my second language.
Title: Re: dq arrays usage
Post by: jj2007 on November 08, 2012, 10:05:15 AM
;MOV aux, OFFSET array   ; you do not want the address...
FLD array      ; you want the value itself

Welcome to the Forum :icon14:
Title: Re: dq arrays usage
Post by: qWord on November 08, 2012, 10:15:51 AM
You should take care of the types and use the formal correct type specifier: REAL4 (=float), REAL8 (=double).
Also intger values must be loaded with FILD or the corresponding integer variant of instructions must be used:
i1 SDWORD 123
...
fild i1
fiadd i2
fistp iResult

REAL10 (long double) and QWORDs can't be used as memory operands and must always loaded and saved with f(i)ld and f(i)st.
Title: Re: dq arrays usage
Post by: Joao Batista on November 08, 2012, 10:39:09 AM
Quote from: jj2007 on November 08, 2012, 10:05:15 AM
;MOV aux, OFFSET array   ; you do not want the address...
FLD array      ; you want the value itself

Welcome to the Forum :icon14:

Oh
My
God

...

Seriously, i can't emphasis this enough... YOU ARE A BEAST!!!!!!

I was struggling with this for 3 days now.. and you fixed it with a single line...
Dude, you rock!!!!

Thanks, thanks, and THANKS!!!!!!!!!!!
Title: Re: dq arrays usage
Post by: Joao Batista on November 08, 2012, 10:41:53 AM
One more question tho.. cause i KNOW i will have problems with it.. how can i store a value on a certain position?

Oh and qWord, y must use dq cause is another constraint from our teacher :S
Title: Re: dq arrays usage
Post by: Joao Batista on November 08, 2012, 10:51:45 AM
Nvm, i could assign a value :D

this is the final code just in case any1 needs it in the future. Thanks again for your help!

.386
.8087
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.data

HelloWorld db "Hello World!", 0
msgOK db "Es igual",0
msgNAK db "No es igual",0


;================================================================================

array dq 5 dup(1.5)
i dq 8
result dq 2.5
pos dq 0
aux dd 0
avalue dq 0
sum dq 1.2

.code
start:

FLD array
FADD i
FSTP pos
FLD qword ptr [pos]
FSTP avalue
FLD avalue
FLD sum
FADD
FSTP qword ptr[pos]
FLD qword ptr [pos]
FSTP avalue
FLD avalue


FLD result
FCOM
fstsw ax
sahf
JE PRINTOK
JMP PRINTNAK



;MOV EAX, OFFSET a
;PUSH EAX
;FLD i
;FADD
;FSTP pos
;FLD qword ptr [pos]
;FSTP aux2
;FLD aux2
;================================================================================


PRINTOK:
    invoke MessageBox, NULL, OFFSET msgOK, OFFSET HelloWorld, MB_OK
    JMP ENDPROGRAM

PRINTNAK:
    invoke MessageBox, NULL, addr msgNAK, addr HelloWorld, MB_OK
    JMP ENDPROGRAM

ENDPROGRAM:
invoke ExitProcess, 0

end start
Title: Re: dq arrays usage
Post by: Joao Batista on November 29, 2012, 09:16:15 AM
Quote from: Joao Batista on November 08, 2012, 10:51:45 AM
Nvm, i could assign a value :D

this is the final code just in case any1 needs it in the future. Thanks again for your help!

.386
.8087
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.data

HelloWorld db "Hello World!", 0
msgOK db "Es igual",0
msgNAK db "No es igual",0


;================================================================================

array dq 5 dup(1.5)
i dq 8
result dq 2.5
pos dq 0
aux dd 0
avalue dq 0
sum dq 1.2

.code
start:

FLD array
FADD i
FSTP pos
FLD qword ptr [pos]
FSTP avalue
FLD avalue
FLD sum
FADD
FSTP qword ptr[pos]
FLD qword ptr [pos]
FSTP avalue
FLD avalue


FLD result
FCOM
fstsw ax
sahf
JE PRINTOK
JMP PRINTNAK



;MOV EAX, OFFSET a
;PUSH EAX
;FLD i
;FADD
;FSTP pos
;FLD qword ptr [pos]
;FSTP aux2
;FLD aux2
;================================================================================


PRINTOK:
    invoke MessageBox, NULL, OFFSET msgOK, OFFSET HelloWorld, MB_OK
    JMP ENDPROGRAM

PRINTNAK:
    invoke MessageBox, NULL, addr msgNAK, addr HelloWorld, MB_OK
    JMP ENDPROGRAM

ENDPROGRAM:
invoke ExitProcess, 0

end start



Hi again.. i thought this was working, but is not..

see... i changed it a little bit, so the code has to recalculate the array position again, and it stopped working.. maybe FLD array isn't getting the array position on memory?

.386
.8087
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.data

HelloWorld db "Hello World!", 0
msgOK db "Es igual",0
msgNAK db "No es igual",0


;================================================================================

array dq 5 dup(1.5)
i dq 8
result dq 2.5
pos dq 0
aux dd 0
avalue dq 0
sum dq 1.0

.code
start:

FLD array
FADD i
FSTP pos
FLD qword ptr [pos]
FSTP avalue
FLD avalue
FLD sum
FADD
FSTP qword ptr[pos]
FLD array
FADD i
FSTP pos
FLD qword ptr [pos]
FSTP avalue
FLD avalue


FLD result
FCOM
fstsw ax
sahf
JE PRINTOK
JMP PRINTNAK



;MOV EAX, OFFSET a
;PUSH EAX
;FLD i
;FADD
;FSTP pos
;FLD qword ptr [pos]
;FSTP aux2
;FLD aux2
;================================================================================


PRINTOK:
    invoke MessageBox, NULL, OFFSET msgOK, OFFSET HelloWorld, MB_OK
    JMP ENDPROGRAM

PRINTNAK:
    invoke MessageBox, NULL, addr msgNAK, addr HelloWorld, MB_OK
    JMP ENDPROGRAM

ENDPROGRAM:
invoke ExitProcess, 0

end start
Title: Re: dq arrays usage
Post by: qWord on November 29, 2012, 09:30:45 AM
i is an integer but you use it like an floating point value. Use FILD/FIADD/... or initalize it with 8.0.
If you want to index  the array:

fld sum ; load crrent value
xor eax,eax ; i = 0
jmp @2
@1:
fadd array[eax*8]
inc eax
@2:
cmp eax,LENGTHOF array
jb @1 ; loop until i < lengthof array
fstp result
Title: Re: dq arrays usage
Post by: Joao Batista on November 29, 2012, 09:40:55 AM
so FLD array actually get the adress of the array?

i tried with 8.0 and still isn't working..

The other code u wrote = chinese :P hehehe

is a task for a subject in the university, and they told me i couldn't use the registers from the cpu. i must work with the coprocessor..

maybe using push offset array?? and adding 8.0 to thaT? but didn't work either :(
Title: Re: dq arrays usage
Post by: qWord on November 29, 2012, 10:06:38 AM
Quote from: Joao Batista on November 29, 2012, 09:40:55 AMis a task for a subject in the university, and they told me i couldn't use the registers from the cpu. i must work with the coprocessor..
That is an impossible task - you probably have misunderstand them.
Title: Re: dq arrays usage
Post by: dedndave on November 29, 2012, 10:08:09 AM
they meant you can't do the math with the registers - they want you to use the FPU for that
they aren't likely to mind if you use registers for addressing an array - a different thing, altogether
Title: Re: dq arrays usage
Post by: Joao Batista on November 29, 2012, 10:19:39 AM
yeah u are probably right.. so how do i get the address with the registers, add to it a double value and store/load something to/from a dq array?

i want to do this:

FLD array address
FLD wanted position as a double
FADD
FSTP pos
FLD qword ptr [pos] ; or FSTP qword ptr [pos]

Title: Re: dq arrays usage
Post by: dedndave on November 29, 2012, 10:29:54 AM
there are many ways to do it
you can even combine registers for multi-dimensional arrays
but, two simple ways to address a linear array...

use the register as a 0-based index and add the array base address
xor ebx,ebx ;EBX indexes the 0th element
Array[ebx]  ;address of element 0
add ebx,4   ;EBX indexes the 1st element
Array[ebx]  ;address of element 1


another approach would be to load the address of the array into EBX
mov ebx,offset Array
[ebx]       ;address of element 0
add ebx,4   ;EBX indexes the 1st element
[ebx]       ;address of element 1
Title: Re: dq arrays usage
Post by: qWord on November 29, 2012, 10:30:13 AM
Quote from: Joao Batista on November 29, 2012, 10:19:39 AM
yeah u are probably right.. so how do i get the address with the registers, add to it a double value and store/load something to/from a dq array?
common, you are study at an university  - go to the library and an take look in to a book about x86 assembly programming. Also, I'm sure that your professor gives you a list with good literature.
Title: Re: dq arrays usage
Post by: Joao Batista on November 29, 2012, 10:38:14 AM
Quote from: qWord on November 29, 2012, 10:30:13 AM
Quote from: Joao Batista on November 29, 2012, 10:19:39 AM
yeah u are probably right.. so how do i get the address with the registers, add to it a double value and store/load something to/from a dq array?
common, you are study at an university  - go to the library and an take look in to a book about x86 assembly programming. Also, I'm sure that your professor gives you a list with good literature.

I know it seems that we haven't tried anything and just came here so you can solve our problems.. but we have been working on this for a while, and our professors suck.. for real..
this is the "good literature" they gave us to work with masm32 : https://docs.google.com/viewer?a=v&pid=sites&srcid=YWx1bW5vcy5leGEudW5pY2VuLmVkdS5hcnxkaXNlbm8tZGUtY29tcGlsYWRvcmVzLWl8Z3g6NWMyZmRlZjhlODE0NzQx (https://docs.google.com/viewer?a=v&pid=sites&srcid=YWx1bW5vcy5leGEudW5pY2VuLmVkdS5hcnxkaXNlbm8tZGUtY29tcGlsYWRvcmVzLWl8Z3g6NWMyZmRlZjhlODE0NzQx)

and every time we look on google how to do something.. we found a million tutorials of how to do it with integers.. and not a single one explaining how to work with doubles... and even when we find em, we can't make them all work.. we only asked you for help with the array instructions and the overflow thing.. but we made an entire compiler from scracth.. using yacc etc..  and we have to finish this before friday :( so .. trust me.. we tried.. we are just not used to work with registers and xor on the daily basis..

I'll try what u are telling us to do.. thank you guys, cause you were really helpful. Everytime we ask for help from our professors they say "your code is wrong" ..

You don't say motherfff..... :) hehe

PS: while u answer we are looking and testing too :P
Title: Re: dq arrays usage
Post by: Joao Batista on November 29, 2012, 11:04:40 AM
ok so this worked:


array dq 5 dup(1.5)
i dd 8
result dq 2.5
pos dd ?
avalue dq 0.0
sum dq 1.0

.code
start:

mov ebx,offset array
add ebx, i

FLD qword ptr [ebx]
FSTP avalue
FLD avalue
FLD sum
FADD
FSTP qword ptr[ebx]

mov ebx,offset array
add ebx, i

FLD qword ptr [ebx]
FSTP avalue
FLD avalue


FLD result
FCOM
fstsw ax
sahf
JE PRINTOK
JMP PRINTNAK



i'm still not sure if i can get i as a dd from the user (cause it may be double result from another arithmetic expression) .. so .. in case i can't .. is there a way i can use a dq as a dd ?

something like dword ptr[dqvar] ?
Title: Re: dq arrays usage
Post by: qWord on November 29, 2012, 11:09:59 AM
Quote from: Joao Batista on November 29, 2012, 11:04:40 AM(cause it may be double result from another arithmetic expression) .. so .. in case i can't .. is there a way i can use a dq as a dd ?
you can convert the double value to an integer using FIST[P] (as long as the value is in range of an integer).
Title: Re: dq arrays usage
Post by: dedndave on November 29, 2012, 11:13:31 AM
the ADD EBX,1 doesn't look right
and - if you did want that, you could just use MOV EBX,offset Array+1
but - you don't - lol

this part looks ok, but i would suggest REAL8 PTR
FLD qword ptr [ebx]

FLD real8 ptr [ebx]
it helps the readability - now, you know at a glance that you are loading a REAL8

generally, i might use QWORD PTR if i were loading an integer
FILD qword ptr [ebx]
loads an 8-byte integer
Title: Re: dq arrays usage
Post by: Joao Batista on November 29, 2012, 12:14:04 PM
mm yeah.. apparently i can't scape of the fact that i need a dd at some point, so i'll just modify my java code, i think it will be easier.

Thanks again guys.. thanks for your help!