Need MSVC with assembler file help

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Michael Sherwin
Posts: 3041
Joined: Fri May 26, 2006 1:00 am
Location: WY, USA
Full name: Michael Sherwin

Need MSVC with assembler file help

Post by Michael Sherwin » Sat Mar 16, 2019 3:18 am

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.
I hate if statements. Pawns demand if statements. Therefore I hate pawns.

jdart
Posts: 3816
Joined: Fri Mar 10, 2006 4:23 am
Location: http://www.arasanchess.org

Re: Need MSVC with assembler file help

Post by jdart » Sat Mar 16, 2019 4:01 am

Are you programming in assembler, or C? If the latter, why do you care what the .asm is?

--Jon

Michael Sherwin
Posts: 3041
Joined: Fri May 26, 2006 1:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: Need MSVC with assembler file help

Post by Michael Sherwin » Sat Mar 16, 2019 5:18 am

jdart wrote:
Sat Mar 16, 2019 4: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?
I hate if statements. Pawns demand if statements. Therefore I hate pawns.

Joost Buijs
Posts: 959
Joined: Thu Jul 16, 2009 8:47 am
Location: Almere, The Netherlands

Re: Need MSVC with assembler file help

Post by Joost Buijs » Sat Mar 16, 2019 7:26 am

Michael Sherwin wrote:
Sat Mar 16, 2019 5:18 am
jdart wrote:
Sat Mar 16, 2019 4: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

Michael Sherwin
Posts: 3041
Joined: Fri May 26, 2006 1:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: Need MSVC with assembler file help

Post by Michael Sherwin » Sat Mar 16, 2019 7:46 am

Joost Buijs wrote:
Sat Mar 16, 2019 7:26 am
Michael Sherwin wrote:
Sat Mar 16, 2019 5:18 am
jdart wrote:
Sat Mar 16, 2019 4: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]?
I hate if statements. Pawns demand if statements. Therefore I hate pawns.

Joost Buijs
Posts: 959
Joined: Thu Jul 16, 2009 8:47 am
Location: Almere, The Netherlands

Re: Need MSVC with assembler file help

Post by Joost Buijs » Sat Mar 16, 2019 8:05 am

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.

mar
Posts: 1992
Joined: Fri Nov 26, 2010 1:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Need MSVC with assembler file help

Post by mar » Sat Mar 16, 2019 10:06 am

Joost Buijs wrote:
Sat Mar 16, 2019 7: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.
Martin Sedlak

Hrvoje Horvatic
Posts: 14
Joined: Mon Nov 10, 2014 12:53 pm

Re: Need MSVC with assembler file help

Post by Hrvoje Horvatic » Sat Mar 16, 2019 10:53 am

Michael Sherwin wrote:
Sat Mar 16, 2019 5: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...

Joost Buijs
Posts: 959
Joined: Thu Jul 16, 2009 8:47 am
Location: Almere, The Netherlands

Re: Need MSVC with assembler file help

Post by Joost Buijs » Sat Mar 16, 2019 11:18 am

mar wrote:
Sat Mar 16, 2019 10:06 am
Joost Buijs wrote:
Sat Mar 16, 2019 7: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.

Michael Sherwin
Posts: 3041
Joined: Fri May 26, 2006 1:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: Need MSVC with assembler file help

Post by Michael Sherwin » Sat Mar 16, 2019 4:12 pm

Hrvoje Horvatic wrote:
Sat Mar 16, 2019 10:53 am
Michael Sherwin wrote:
Sat Mar 16, 2019 5: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.
I hate if statements. Pawns demand if statements. Therefore I hate pawns.

Post Reply