News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

dq arrays usage

Started by Joao Batista, November 08, 2012, 09:54:46 AM

Previous topic - Next topic

Joao Batista

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.

jj2007

;MOV aux, OFFSET array   ; you do not want the address...
FLD array      ; you want the value itself

Welcome to the Forum :icon14:

qWord

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.
MREAL macros - when you need floating point arithmetic while assembling!

Joao Batista

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!!!!!!!!!!!

Joao Batista

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

Joao Batista

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

Joao Batista

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

qWord

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
MREAL macros - when you need floating point arithmetic while assembling!

Joao Batista

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 :(

qWord

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.
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

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

Joao Batista

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]


dedndave

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

qWord

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.
MREAL macros - when you need floating point arithmetic while assembling!

Joao Batista

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

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