Can you still define a struct in Assembly?

Using Xcode 15.2, x86_64, and for the life of me, I can't figure out how to declare a data structure in assembly.

All the "current"ish references I've read for Clang LLVM & "as" assembly have no references to data structure directives. Tons of different types of sections, but I haven't seen any that would apply.

I've looked at .bss, and while I can defines different fields in it, it still reserves space in the program, and I don't need space reserved - I just need a data structure definition so I'm not using magic numbers.

Perhaps there is a utility to convert a C struct to an assembly struct that I haven't seen? Surely there has to be a way to define a data structure in assembly.

What’s with all the assembly questions today? (-:

Perhaps there is a utility to convert a C struct to an assembly struct that I haven't seen?

Not that I’m aware of. It kinda depends on your goal. If you want to use these structures entirely within your assembly language code, there are various tricks you can apply. However, if you want your struct to exactly match the layout of a C struct, things get a bit complex:

  • If you’re building for the local machine, you can write a C program that outputs the assembly definitions for the various member offsets.

  • If you’re cross compiling… hmmm… I’m not sure what you’d do in that case.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

My goal is to have standalone assembly programs. However, when structs or data items are returned from OS calls, I want to be able to map those accurately (and painlessly) as well.

What are the tricks you refer to? Writing a C program to write out equates for field offsets would technically work, but it's pretty ugly and a PITA to maintain, as it's really just magic numbers at that point.

I saw in the last version of Xcode (the version that was associated with El Capitan) I could choose NASM instead of the MAC OS assembler. Should I be using NASM instead? I haven't looked to see if that's still an option in 15.2. I've perused the build options but didn't see it yet. (I'm on Ventura now).

My research into the lack off support for structs/unions in the Mac OS assembler harkens of a big road block sign that says "DO NOT ENTER", as without support for structures, it's really not a viable development option. I've written in assembler since 1984, but not on this platform.

As a side question... I'd be up for upgrading to a MacBook Pro, and writing assembler for the ARM instead of Intel, but will I hit the same native lack of support there too?

Thanks!

Can you still define a struct in Assembly?
 
 
Q