Page 1 of 3

Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 4:18 am
by Michael Sherwin
I have these structures

typedef struct {
int sqr; // the square the piece is on
int typ; // the type of piece, WP ... BK
int clr; // the side
int prv; // the previous on board piece
int nxt; // the next on board piece
int val; // the value of the piece
} ps; // piece-structure

typedef struct {
int wtm; // white-to-move
int ply; // search ply
int castle; // castling status
int epsq; // capture e.p. square if any
int fifty; // fifty move counter
int board[64]; // board for this thread
ps p[40]; // index piece array
ss s[100]; // search stack
} ts; // thread-structure

ts *t;

This call to malloc in main.

t = (ts *)malloc(sizeof(ts));

This prototype

extern int MoveGen(ts *);

This call

MoveGen(t);

This in the .asm file

.model flat

.data

.code

public c MoveGen

MoveGen:
push ebp
mov ebp, esp
mov ebx, [ebp+8]

; Let's say I want to load t->p[0].nxt into ecx
; How do I access t->p[0].nxt ?

mov esp, ebp
pop ebp
ret

end

I looked on the internet all day and found nothing. I have verified that the pointer t is on the stack and did get moved to ebx.

Re: Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 5:01 am
by jdart
Are you programming in assembler, or C? If the latter, why do you care what the .asm is?

--Jon

Re: Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 6:18 am
by Michael Sherwin
jdart wrote: Sat Mar 16, 2019 5:01 am Are you programming in assembler, or C? If the latter, why do you care what the .asm is?

--Jon
I am programming in both. Some functions like initialization functions and engine protocol functions are in C. Search functions will be in assembler. MSVC can compile both kind of files and link them. I know how to access non nested C structures from the .asm file. But nowhere shows me how to access nested structures. Can someone please help?

Re: Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 8:26 am
by Joost Buijs
Michael Sherwin wrote: Sat Mar 16, 2019 6:18 am
jdart wrote: Sat Mar 16, 2019 5:01 am Are you programming in assembler, or C? If the latter, why do you care what the .asm is?

--Jon
I am programming in both. Some functions like initialization functions and engine protocol functions are in C. Search functions will be in assembler. MSVC can compile both kind of files and link them. I know how to access non nested C structures from the .asm file. But nowhere shows me how to access nested structures. Can someone please help?
There is no difference between accessing a non nested struct or a nested struct, the only thing you have to do is to calculate the offset for the element you want to access. The offset depends upon the alignment the compiler uses for structures, the default alignment MSVC uses for structures is 8 bytes, since you only seem to use 32 bit integers in your structs it should be very straightforward to calculate the proper offset.

Here is some documentation about how MSVC aligns and packs structures:
https://docs.microsoft.com/en-us/cpp/cp ... atapacking

Re: Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 8:46 am
by Michael Sherwin
Joost Buijs wrote: Sat Mar 16, 2019 8:26 am
Michael Sherwin wrote: Sat Mar 16, 2019 6:18 am
jdart wrote: Sat Mar 16, 2019 5:01 am Are you programming in assembler, or C? If the latter, why do you care what the .asm is?

--Jon
I am programming in both. Some functions like initialization functions and engine protocol functions are in C. Search functions will be in assembler. MSVC can compile both kind of files and link them. I know how to access non nested C structures from the .asm file. But nowhere shows me how to access nested structures. Can someone please help?
There is no difference between accessing a non nested struct or a nested struct, the only thing you have to do is to calculate the offset for the element you want to access. The offset depends upon the alignment the compiler uses for structures, the default alignment MSVC uses for structures is 8 bytes, since you only seem to use 32 bit integers in your structs it should be very straightforward to calculate the proper offset.

Here is some documentation about how MSVC aligns and packs structures:
https://docs.microsoft.com/en-us/cpp/cp ... atapacking
Okay thank you. So no need to use the dot operator like all examples I looked at. Just do something like this mov ecx, [ebx+number_int*8]?

Re: Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 9:05 am
by Joost Buijs
Yes, basically that is it. With inline assembly you can have the compiler handle this for you, unfortunately MSVC doesn't support inline assembly in 64 bit mode.

Maybe there are also ways to access labels and declarations in C from external assembly, that is something I can't tell because I never used it and I'm not an expert on this field. The last time I used assembly is at least 30 years back.

Re: Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 11:06 am
by mar
Joost Buijs wrote: Sat Mar 16, 2019 8:26 am the default alignment MSVC uses for structures is 8 bytes, since you only seem to use 32 bit integers in your structs it should be very straightforward to calculate the proper offset.
Sorry, but this is not true. The default alignment is simply the alignment of a member that requires maximum aligment.

So if you have a struct containing one uint8_t, it's alignment is 1 byte. If you only have uint32_ts, it's 4 bytes and so on.
The size of the structure will also be padded so that it's size is divisible by its alignment, so a struct with uint64_t and uint8_t will
have an alignment of 8 and size of 16 bytes.
Intermixing members with different alignment requirements is also straightforward - simply pad to the required alignment before adding a new member.
This behavior is not really compiler-specific, all compilers behave this way (unless you use the evil #pragma pack, which I would discourage - those who ever wrote for ARM understand why), because otherwise it would break ABI. Also, no shuffling of members is allowed by the compiler either for the same reason.

I guess you meant the default alignment for a dynamically allocated memory, then yes, that would be at least 8 bytes.

Re: Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 11:53 am
by Hrvoje Horvatic
Michael Sherwin wrote: Sat Mar 16, 2019 6:18 am
I am programming in both. Some functions like initialization functions and engine protocol functions are in C. Search functions will be in assembler. MSVC can compile both kind of files and link them. I know how to access non nested C structures from the .asm file. But nowhere shows me how to access nested structures. Can someone please help?
Super-simple solution that works EVERY time is to force MSVC to generate .asm output file from c/c++ source code, and then you inspect generated asm code...

In project properties dialog, go for C/C++ -> Output Files -> Assembler output, and put something like "Assembly With Source Code (/FAs)" so you get C/C++ source code in comment, together with asm code...

Notice that many "weird" things happen when you turn on optimization (many things "dissapear", and some new things "appear"), so for the starters don't turn it on...

Re: Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 12:18 pm
by Joost Buijs
mar wrote: Sat Mar 16, 2019 11:06 am
Joost Buijs wrote: Sat Mar 16, 2019 8:26 am the default alignment MSVC uses for structures is 8 bytes, since you only seem to use 32 bit integers in your structs it should be very straightforward to calculate the proper offset.
Sorry, but this is not true. The default alignment is simply the alignment of a member that requires maximum aligment.

So if you have a struct containing one uint8_t, it's alignment is 1 byte. If you only have uint32_ts, it's 4 bytes and so on.
The size of the structure will also be padded so that it's size is divisible by its alignment, so a struct with uint64_t and uint8_t will
have an alignment of 8 and size of 16 bytes.
Intermixing members with different alignment requirements is also straightforward - simply pad to the required alignment before adding a new member.
This behavior is not really compiler-specific, all compilers behave this way (unless you use the evil #pragma pack, which I would discourage - those who ever wrote for ARM understand why), because otherwise it would break ABI. Also, no shuffling of members is allowed by the compiler either for the same reason.

I guess you meant the default alignment for a dynamically allocated memory, then yes, that would be at least 8 bytes.
It seems you are right, the documentation is a bit fuzzy about this. The default alignment is '/Zp8', for structure members it is the minimum of it's size and the packing setting provided by /Zpn. This is something you can easily overlook.

Re: Need MSVC with assembler file help

Posted: Sat Mar 16, 2019 5:12 pm
by Michael Sherwin
Hrvoje Horvatic wrote: Sat Mar 16, 2019 11:53 am
Michael Sherwin wrote: Sat Mar 16, 2019 6:18 am
I am programming in both. Some functions like initialization functions and engine protocol functions are in C. Search functions will be in assembler. MSVC can compile both kind of files and link them. I know how to access non nested C structures from the .asm file. But nowhere shows me how to access nested structures. Can someone please help?
Super-simple solution that works EVERY time is to force MSVC to generate .asm output file from c/c++ source code, and then you inspect generated asm code...

In project properties dialog, go for C/C++ -> Output Files -> Assembler output, and put something like "Assembly With Source Code (/FAs)" so you get C/C++ source code in comment, together with asm code...

Notice that many "weird" things happen when you turn on optimization (many things "dissapear", and some new things "appear"), so for the starters don't turn it on...
So are you saying to write it in C first. Then look at the assembly code the compiler outputs and do it the same way in the .asm file? I do not think that will work because every change in the code causes changes in the addresses of everything else.