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

coder

  • Member
  • **
  • Posts: 103
Re: Using MASM's DSEG CSEG 32-bit
« Reply #15 on: April 29, 2017, 01:25:31 PM »
Interesting that a DSEG/CSEG allows you to set the alignment manually (although it's aligned to page anyway). Below is a modified version for use with XMM instead for those who don't have AVX. Added SSEG. All sections are page-aligned (which also aligned to 32). I think it's more convenient this way for any alignment larger than 16.

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 ALIGN(32) PUBLIC '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, t = %p',10,0
DSEG ENDS

SSEG SEGMENT STACK 'STACK'
t dq 0
SSEG ENDS

CSEG SEGMENT PUBLIC 'CODE'
WinMain@16 proc
enter 0,0
invoke printf,addr confirm,addr x, addr WinMain@16, addr t
movdqa xmm0,xmmword ptr x
movdqa xmm1,xmmword ptr z
shufpd xmm1,xmm0,0
movq v1,xmm1
invoke printf,addr outstring,v1
leave
ret
WinMain@16 endp
CSEG ENDS
END

The output for all three markers

Code: [Select]
x = 00405000, WinMain@16 = 00403000, t = 00406000
89.230000

I'm just not sure if SSEG really mean anything to ESP though.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4933
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Using MASM's DSEG CSEG 32-bit
« Reply #16 on: April 29, 2017, 04:58:18 PM »
The crude technique for testing if a variable (esp based) is aligned correctly for an SSE or AVX instruction is to make a test piece and use an instruction that must be aligned to a minimum boundary. You will find out soon enough if its correct or not.  :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

coder

  • Member
  • **
  • Posts: 103
Re: Using MASM's DSEG CSEG 32-bit
« Reply #17 on: April 29, 2017, 09:33:41 PM »
Yea hutch. I still can't figure out what SSEG has anything to do with ESP. Using ASSUME SS don't seem to help either. ESP still aligns 4.

 

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4933
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Using MASM's DSEG CSEG 32-bit
« Reply #18 on: April 29, 2017, 10:31:22 PM »
This should do the job.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .data?
      __esp dd ?


    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    call aligned

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

aligned proc

    mov __esp, esp          ; save ESP

    memalign esp, 1024
    sub esp, 1024

  ; -------------------------------
  ; see what your alignment is here
  ; -------------------------------

    mov esp, __esp          ; restore ESP

    ret

aligned endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

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

nidud

  • Member
  • *****
  • Posts: 1411
    • https://github.com/nidud/asmc
Re: Using MASM's DSEG CSEG 32-bit
« Reply #19 on: April 29, 2017, 11:05:30 PM »
Code: [Select]
  local ymm[4*32+32]:byte

lea ebx,ymm[31]
and ebx,-32

vmovdqa [ebx+0*32],ymm0
vmovdqa [ebx+1*32],ymm1
vmovdqa [ebx+2*32],ymm2
vmovdqa [ebx+3*32],ymm3

...

vmovdqa ymm0,[ebx+0*32]
vmovdqa ymm1,[ebx+1*32]
vmovdqa ymm2,[ebx+2*32]
vmovdqa ymm3,[ebx+3*32]