Author Topic: Using MASM's DSEG CSEG 32-bit  (Read 812 times)

coder

  • Member
  • **
  • Posts: 103
Using MASM's DSEG CSEG 32-bit
« on: April 29, 2017, 01:54:57 AM »
Hi folks. Once in a while, I brave myself to test 32-bit MASM code. One attempt was to use ALIGN 32 (to use the YMMs) in my code. Turns out there's no easy way to do it in 32-bit MASM by using my normal .code .data setting. So after an hour of digging, I found the use DSEG/CSEG from the internet. I heard it the first time today. LOL. I tried it out and it didn't complain of alignment at all.

Code: [Select]
.xmm
.model flat,c

extrn printf:proc
option casemap:none

DSEG SEGMENT PARA PUBLIC 'DATA'
x dq 56.334,10.436,11.982,71.324
z dq 89.123,90.123,11.128,55.567
outstring db '%f',10,0
val1 dq 0.0
DSEG ENDS

CSEG SEGMENT PUBLIC 'CODE'
WinMain@16 proc
push ebp
mov ebp,esp
vmovdqa ymm0,ymmword ptr[x]
vmovdqa ymm1,ymmword ptr[z]
vshufpd ymm2,ymm1,ymm0,0
movq val1,xmm2
push dword ptr val1+4
push dword ptr val1
push offset outstring
call printf
add esp,12
leave
ret
WinMain@16 endp
CSEG ENDS
END

It works, but still I don't quite understand how it works and I don't know what questions to ask either. Something must be wrong somewhere because I got it correct the first time. LOL LOL. Care to give me as much information as possible on this type of code configuration? Really appreciate your help.

 
 

jj2007

  • Member
  • *****
  • Posts: 7756
  • Assembler is fun ;-)
    • MasmBasic
Re: Using MASM's DSEG CSEG 32-bit
« Reply #1 on: April 29, 2017, 02:17:07 AM »
Doesn't look very aligned ::)

Code: [Select]
include \masm32\include\masm32rt.inc

DSEG SEGMENT PARA PUBLIC 'DATA'
  var1 dd 111h
DSEG ENDS
DSEG SEGMENT PARA PUBLIC 'DATA'
  var2 dd 111h
DSEG ENDS
DSEG SEGMENT PARA PUBLIC 'DATA'
  var3 dd 111h
DSEG ENDS

.code
start:
  mov eax, offset var1
  print str$(eax), 13, 10
  mov ecx, offset var2
  print str$(ecx), 13, 10
  mov edx, offset var3
  print str$(edx), 13, 10
  exit

end start

Output:
4210688
4210692
4210696

nidud

  • Member
  • *****
  • Posts: 1411
    • https://github.com/nidud/asmc
Re: Using MASM's DSEG CSEG 32-bit
« Reply #2 on: April 29, 2017, 02:18:49 AM »
The OS will load your application on a 16-byte aligned address. This may from time to time be 32 or higher but this will be random so you have to apply alignment-code to be sure it's properly aligned to any granularity above 16.

coder

  • Member
  • **
  • Posts: 103
Re: Using MASM's DSEG CSEG 32-bit
« Reply #3 on: April 29, 2017, 02:19:00 AM »
Hi JJ. But you see, the code works though. I am on Win7 32-bit (64-bit PC). No complain of segfault at all. Curious though.

 

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4934
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Using MASM's DSEG CSEG 32-bit
« Reply #4 on: April 29, 2017, 02:21:18 AM »
I can't get it to build in 32 bit MASM.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

coder

  • Member
  • **
  • Posts: 103
Re: Using MASM's DSEG CSEG 32-bit
« Reply #5 on: April 29, 2017, 02:21:35 AM »
The OS will load your application on a 16-byte aligned address. This may from time to time be 32 or higher but this will be random so you have to apply alignment-code to be sure it's properly aligned to any granularity above 16.

Hi Nidud. Has the word 'para' anything to do with it? Is that a page boundary?

I applied "USE32" to DSEG moments ago, now it complains something else. Confusing.

coder

  • Member
  • **
  • Posts: 103
Re: Using MASM's DSEG CSEG 32-bit
« Reply #6 on: April 29, 2017, 02:22:50 AM »
I can't get it to build in 32 bit MASM.
What's the complain Hutch? Misalignment or something else? Works on mine though.

coder

  • Member
  • **
  • Posts: 103
Re: Using MASM's DSEG CSEG 32-bit
« Reply #7 on: April 29, 2017, 02:24:49 AM »
Owh, I link with "gcc -m32 myprog.obj -o myprog.exe" just to be clear.

coder

  • Member
  • **
  • Posts: 103
Re: Using MASM's DSEG CSEG 32-bit
« Reply #8 on: April 29, 2017, 02:29:05 AM »
In both cases, I can't use ALIGN 32, except by manual padding after ALIGN 16.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4934
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Using MASM's DSEG CSEG 32-bit
« Reply #9 on: April 29, 2017, 02:35:38 AM »
I have got this much of it to build. RE : data alignment for the YMM code, allocate dynamic memory, align it then write you data to it.
Code: [Select]
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .686p                     ; maximum processor model
    .XMM                      ; SIMD extensions
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive
 
  ; ---------------------------------------------------------
  ; Write the prototype for the procedure name below ensuring
  ; that the parameter count & size match and put it in your
  ; library include file.
  ; ---------------------------------------------------------

    .data       ; initialised data section
      x dq 56.334,10.436,11.982,71.324
      z dq 89.123,90.123,11.128,55.567
      outstring db '%f',10,0
      val1 dq 0.0

    .code       ; code section

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

;;; extrn printf:proc

.code

WinMain@16 proc
push ebp
mov ebp,esp
vmovdqa ymm0,ymmword ptr[x]
vmovdqa ymm1,ymmword ptr[z]
vshufpd ymm2,ymm1,ymm0,0
movq val1,xmm2
 ; push dword ptr val1+4
 ; push dword ptr val1
 ; push offset outstring
 ; call printf
add esp,12
leave
ret
WinMain@16 endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 7756
  • Assembler is fun ;-)
    • MasmBasic
Re: Using MASM's DSEG CSEG 32-bit
« Reply #10 on: April 29, 2017, 02:41:56 AM »
Hi JJ. But you see, the code works though. I am on Win7 32-bit (64-bit PC). No complain of segfault at all. Curious though.

My first assumption there was that every time you create a new segment it starts at a 512-byte border. Apparently the linkers are clever enough to prevent this - unless there is another trick. There is another thread on align macros but can't find it right now. It's messy, be careful.

coder

  • Member
  • **
  • Posts: 103
Re: Using MASM's DSEG CSEG 32-bit
« Reply #11 on: April 29, 2017, 03:42:17 AM »
Not sure JJ. I think Windows has a fixed ImageBase policy. With that in mind, I can safely assume that every new sections or segments are allocated page-aligned memory. So whether it's a .data or DSEG, alignment doesn't really become a concern at offset 0 (first data). 512-bytes is a flat layout (no section or segment), IMHO.




nidud

  • Member
  • *****
  • Posts: 1411
    • https://github.com/nidud/asmc
Re: Using MASM's DSEG CSEG 32-bit
« Reply #12 on: April 29, 2017, 03:46:02 AM »
Has the word 'para' anything to do with it? Is that a page boundary?

No. PARA means paragraph (16). This is the segment alignment size for the .x86 hardware.

coder

  • Member
  • **
  • Posts: 103
Re: Using MASM's DSEG CSEG 32-bit
« Reply #13 on: April 29, 2017, 03:48:24 AM »
I made the code shorter for easy reference.

Code: [Select]
;ml /c /coff myprog.asm
;gcc -m32 myprog.obj -o myprog.exe
.686p
.xmm
.model flat,c
option casemap:none

printf PROTO C:vararg

;DSEG SEGMENT PAGE PUBLIC 'DATA'
.data
x  dq 56.33,10.36,11.98,71.24
z  dq 89.23,90.12,11.28,55.57
v1 dq 0.0
outstring db '%f',10,0
confirm db 'x = %p, WinMain@16 = %p',10,0
;DSEG ENDS

;CSEG SEGMENT PUBLIC 'CODE'
.code
WinMain@16 proc
enter 0,0
invoke printf,addr confirm,addr x, addr WinMain@16
vmovdqa ymm0,ymmword ptr x
vmovdqa ymm1,ymmword ptr z
vshufpd ymm2,ymm1,ymm0,0
movq v1,xmm2
invoke printf,addr outstring,v1
leave
ret
WinMain@16 endp
;CSEG ENDS
END

The output for CSEG and DSEG are page-aligned, but not so with .code and .data

Output with CSEG/DSEG pair
Code: [Select]
x = 00405000, WinMain@16 = 00403000
89.230000

Output with .code/.data pair
Code: [Select]
x = 00403010, WinMain@16 = 004026E0
;missing output due to misalignment

jj2007

  • Member
  • *****
  • Posts: 7756
  • Assembler is fun ;-)
    • MasmBasic
Re: Using MASM's DSEG CSEG 32-bit
« Reply #14 on: April 29, 2017, 04:32:06 AM »
Not sure JJ. I think Windows has a fixed ImageBase policy. With that in mind, I can safely assume that every new sections or segments are allocated page-aligned memory.

Yes, that's right. Except that the bloody linker doesn't give you a new segment, as demonstrated in reply #1. Instead, it merges several segments into one. That's why the addresses of the variables increase only by a DWORD.