News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

How many registers do I have?

Started by frktons, July 15, 2010, 04:44:02 PM

Previous topic - Next topic

frktons

Quote from: clive on July 15, 2010, 08:01:46 PM
Microsoft (R) Macro Assembler Version 6.15.8803     07/15/10 15:01:09
test32.asm      Page 1 - 1


        .686
        .XMM
        .MODEL FLAT

00000000         .CODE

00000000 _start:

00000000  B9 00003039         mov     ecx, 12345
00000005  0F 6E C1         movd    mm0,ecx
00000008  0F 7E C1         movd    ecx,mm0

0000000B  C3         ret

        END     _start


OK clive, I got that.
What if I want to display the content of ecx and mm0?
Why my example is not assembling? What's wrong?

Quote from: clive on July 15, 2010, 07:58:15 PM
The general take away is that using MMX or SSE registers as additional scratch registers for the processor is not a great plan.

Specifically, they are designed to pull vector data out of memory and process it/them in parallel. Intel calls this SIMD (Single Instruction Multiple Data)

The Software Optimization Cookbook, Richard Gerber, Intel Press, ISBN 0-9712887-1-4
http://www.alibris.com/booksearch?qisbn=9780971288713&qwork=

Good to know. If I'd like a faster code I'll have to take that into account.  :U
Mind is like a parachute. You know what to do in order to use it :-)

frktons

Well, one of the thing MASM32 doesn't like is:

   print     str$(mm0)," value of mm0",13,10


maybe the print macro doesn't accept this kind of
data to be displayed.

What else?


Microsoft (R) Macro Assembler Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

Assembling: C:\masm32\examples\mmx_usage.asm
C:\masm32\examples\mmx_usage.asm(1) : error A2044:invalid character in file
C:\masm32\examples\mmx_usage.asm(37) : error A2034:must be in segment block
C:\masm32\examples\mmx_usage.asm(39) : error A2034:must be in segment block
C:\masm32\examples\mmx_usage.asm(40) : error A2034:must be in segment block
C:\masm32\examples\mmx_usage.asm(42) : error A2034:must be in segment block
C:\masm32\examples\mmx_usage.asm(49) : error A2006:undefined symbol : start
C:\masm32\examples\mmx_usage.asm(49) : error A2148:invalid symbol type in expres
sion : start
_
Assembly Error
Premere un tasto per continuare . . .


Well I found something else, I was missing .data and .code

Still something wrong:

C:\masm32\examples\mmx_usage.asm(1) : error A2044:invalid character in file


And the last one: I was missing a ";" at the very first line of comment.
Mind is like a parachute. You know what to do in order to use it :-)

BogdanOntanu

Quote from: frktons on July 15, 2010, 08:23:47 PM
maybe the print macro doesn't accept this kind of
data to be displayed.

What else?

Well, many times in ASM you are on your own and you have to create your own tools and routines (unlike in HLL languages).

MASM32 spoils you a little with it's stock of macro's and routines ready to use BUT it is possible that the macro provided with MASM32 does not support 64bits or 128 bits registers printing... after all it is designed for 32 bits.

Hence there might be nothing else.

Use the example kindly provided by Clive and consider that maybe now it is a good time for you to learn how to write your own simple routine to convert an 64bits / 128 bits integer into it's ASCII equivalent and to print the resulting string on screen.

Alternatively you could store the MMX / XMM register into a memory location / variable and then do two consecutive prints on it's low and high parts inorder to see the ASCII on screen and avoid writting your own code ...


Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

clive

Quote from: frktons
C:\masm32\examples\mmx_usage.asm(37) : error A2034:must be in segment block
C:\masm32\examples\mmx_usage.asm(49) : error A2006:undefined symbol : start

Needs a .CODE, _start is probably what you should be using. Pretty sure you can't use MM0..MM7 for those STR$ macros, being as the registers live inside the FPU.
It could be a random act of randomness. Those happen a lot as well.

frktons

#19
Quote from: BogdanOntanu on July 15, 2010, 08:34:55 PM

Well, many times in ASM you are on your own and you have to create your own tools and routines (unlike in HLL languages).

MASM32 spoils you a little with it's stock of macro's and routines ready to use BUT it is possible that the macro provided with MASM32 does not support 64bits or 128 bits registers printing... after all it is designed for 32 bits.

Hence there might be nothing else.

Use the example kindly provided by Clive and consider that maybe now it is a good time for you to learn how to write your own simple routine to convert an 64bits / 128 bits integer into it's ASCII equivalent and to print the resulting string on screen.

Alternatively you could store the MMX / XMM register into a memory location / variable and then do two consecutive prints on it's low and high parts inorder to see the ASCII on screen and avoid writting your own code ...


Thanks, I'll try do do it by myself.
For the time being I realized how I can use the MMX registers,
and this is the first thing I was trying to understand.
The final code that compiles and runs could be the starting
point for doing what you suggest.  :P

;««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««« *
; Example of MMX register usage
;««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««« *
   include \masm32\include\masm32rt.inc
   .686  
   .mmx
   .xmm

   include \masm32\macros\macros.asm       ; MASM support macros

    .data

    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                          ; The CODE entry point to the program

   mov      ecx, 12345
   movd     mm0,ecx
 ;  print    str$(mm0)," value of mm0",13,10
   movd     ecx,mm0  
   print    str$(ecx)," value of ecx",13,10  
   print "Press a key to close the program"
   call wait_key
   print chr$(13,10)

   exit

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

end start                       ; Tell MASM where the program ends


Quote from: clive on July 15, 2010, 09:08:33 PM
Quote from: frktons
C:\masm32\examples\mmx_usage.asm(37) : error A2034:must be in segment block
C:\masm32\examples\mmx_usage.asm(49) : error A2006:undefined symbol : start

Needs a .CODE, _start is probably what you should be using. Pretty sure you can't use MM0..MM7 for those STR$ macros, being as the registers live inside the FPU.

Yes clive, after some experiments I got that, now I'll try to find a way to display
the mm0 content.  :U

Edit: something like this would be sufficient in this case:

    mov      ecx, 12345
    movd     mm0, ecx
    movd     eax, mm0
    print    str$(eax)," value of 32 lower bit mm0",13,10
Mind is like a parachute. You know what to do in order to use it :-)

MichaelW

Printf can handle 64-bit integers directly, and the formatting is easy. For 128-bit values the only easy method I can see is to display them as back to back 64-bit values, in hex.

;==============================================================================
    include \masm32\include\masm32rt.inc
    .586
    .MMX
    .XMM
;==============================================================================
    .data
      i64     dq 1122334455667788h
              dq 8877665544332211h
      rmm0    dq 0
      rxmm0   dq 0,0
    .code
;==============================================================================
start:
;==============================================================================
    movq mm0, i64
    movq rmm0, mm0
    movups xmm0, i64
    movups rxmm0, xmm0

    invoke crt_printf, cfm$("MM0 = %I64Xh\n\n"), rmm0
    invoke crt_printf, cfm$("XMM0 = %I64X%I64Xh\n\n"), rxmm0, rxmm0+8

    inkey "Press any key to exit..."
    exit
;==============================================================================
end start

eschew obfuscation

ecube

GoASM is the best, the more I use it, the more I love it, it-just-makes-sense, and it saves me a ton of time even in the 32bit world vs masm.

frktons

#22
Quote from: MichaelW on July 16, 2010, 05:26:17 AM
Printf can handle 64-bit integers directly, and the formatting is easy. For 128-bit values the only easy method I can see is to display them as back to back 64-bit values, in hex.

;==============================================================================
    include \masm32\include\masm32rt.inc
    .586
    .MMX
    .XMM
;==============================================================================
    .data
      i64     dq 1122334455667788h
              dq 8877665544332211h
      rmm0    dq 0
      rxmm0   dq 0,0
    .code
;==============================================================================
start:
;==============================================================================
    movq mm0, i64
    movq rmm0, mm0
    movups xmm0, i64
    movups rxmm0, xmm0

    invoke crt_printf, cfm$("MM0 = %I64Xh\n\n"), rmm0
    invoke crt_printf, cfm$("XMM0 = %I64X%I64Xh\n\n"), rxmm0, rxmm0+8

    inkey "Press any key to exit..."
    exit
;==============================================================================
end start



Hi Michael, thanks for the example.

I've tried to assemble it with MASM32 but I get some errors:

Microsoft (R) Macro Assembler Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

Assembling: C:\masm32\examples\print64bit.asm
C:\masm32\examples\print64bit.asm(20) : error A2070:invalid instruction operands

C:\masm32\examples\print64bit.asm(21) : error A2070:invalid instruction operands

_
Assembly Error
Press a key . . .


What's wrong?

It is pointing at these instructions I think:

    movups xmm0, i64
    movups rxmm0, xmm0


because I added a couple of comment lines on the top.

Quote from: E^cube on July 16, 2010, 05:49:41 AM
GoASM is the best, the more I use it, the more I love it, it-just-makes-sense, and it saves me a ton of time even in the 32bit world vs masm.

Well, GoAsm could be a next tool to get, but actually I
prefer to stick with MASM32 in order to learn enough bits
of Assembly, afterwhile I'll see....
Mind is like a parachute. You know what to do in order to use it :-)

MichaelW

QuoteI've tried to assemble it with MASM32 but I get some errors

I tested with ML 6.15 only. Try adding OWORD PTR in front of the memory operands.

http://msdn.microsoft.com/en-us/library/2det2cf1(VS.71).aspx
eschew obfuscation

frktons

#24
Quote from: MichaelW on July 16, 2010, 12:20:54 PM

I tested with ML 6.15 only. Try adding OWORD PTR in front of the memory operands.

http://msdn.microsoft.com/en-us/library/2det2cf1(VS.71).aspx

Thanks Michael it now works this way:

    movups xmm0, OWORD PTR i64
    movups OWORD PTR rxmm0,  xmm0


and outputs:


MM0 = 1122334455667788h

XMM0 = 11223344556677888877665544332211h

Press any key to exit...


Is that what we expected? OWORD means we are using 8 bytes operands?
Mind is like a parachute. You know what to do in order to use it :-)

MichaelW

Yes, and yes. I initially included the OWORD PTR because the defined size of the data does not match the register size. I removed it when 6.15 did not complain, but that clearly was a bad choice.
eschew obfuscation

frktons

Quote from: MichaelW on July 16, 2010, 03:05:21 PM
Yes, and yes. I initially included the OWORD PTR because the defined size of the data does not match the register size. I removed it when 6.15 did not complain, but that clearly was a bad choice.

:U
Mind is like a parachute. You know what to do in order to use it :-)

frktons

According to these small experiments and code kindly posted
by some of you, I can now reply to my own question that there
are quite a few registers I can use on my box,  8 general purpose
registers, 8 MMX, 16 XMM and probably some more.

How to use them, and when it is convenient to do it, well that
is a long path to go  :P
Mind is like a parachute. You know what to do in order to use it :-)

BogdanOntanu

Quote from: frktons on July 16, 2010, 09:22:26 PM
16 XMM and probably some more.

In 32 bits mode you only have access to 8 XMM registers.

In 64bits mode you also have access to 16 GPR registers.

Quote
How to use them, and when it is convenient to do it, well that
is a long path to go  :P

This is not really important from an conceptual point of view.

Most software algorithms can be expressed and perform very well with just a few GPR registers available. At some point the number of registers becomes a market issue and a drag on the CPU speed (selecting from 16 registers instead of 8 registers is slower in electronics).

Also, internally the CPU does perform a few extra tricks like register alias/renaming and even if you overuse the same register the CPU knows better.

Hence, for a start I would not concentrate myself too much on using registers intensively.

It is more important to learn how to express code and algorithms in a "register / memory / jumps / calls and returns"  based "world"  rather that to contemplate optimum register usage.

For an syntax example understanding the diference between EAX and [EAX] is much more important ... and to a certain extent the difference between OFFSET and ADDR operators is also important.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

hutch--

Frank,

With registers in 32 bit, about 95% of the work is done in the 8 GP registers. The additional registers with their matching instruction sets tend to be more focused on a particularl type of work. FP for maths, MMX sharing the FP registers was the early multi-media extensions that were then bypassed by the extended multi-media instructions and registers (XMM or SIMD) which has then developed through about 4 families of additions (SSE - SSE4.2 Intel ).

Each register type has its own instruction set and the most extensive are the original general purpose integer registers (EAX ECX EDX EBX EBP ESP ESI EDI). 32 bit and later processors removed many restrictions on how the 8 GP registers were used but some legacy instructions still require specific registers to work, XLAT MOVS STOS etc ....
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php