flat assembler
Message board for the users of flat assembler.

Index > Main > 64-bit integer arithmetic / using long mode

Author
Thread Post new topic Reply to topic
john_s



Joined: 31 May 2010
Posts: 5
Location: UK
john_s 31 May 2010, 13:39
I'm trying to do some number-theory work - primes et cetera - using large numbers. Needing more speed than I can get with high-level languages, I figured that assembler would be the way to go. Unfortunately, machine-code syntax has moved on a long way since my last efforts (Z80 processor about 30 years ago) Sad

The coding I want to do should be quite simple, but I keep hitting roadblocks in the 64-bit instructions. Simple steps like loading a value into a 64-bit register just aren't working.

I think a lot of my trouble comes from not knowing what the registers are called. My system has an AMD Phenom 8450 chip. All I can get from AMD is "sixteen 64-bit integer registers, sixteen 128-bit SSE/SSE2/SSE3 registers." No names given...

I seem to have MM0-MM7 registers, but not MM8-MM15. I should be able to clear RAX with a PXOR statement, then load a value into EAX. Doesn't work...

After some weeks of struggle, I'm about ready to give up, having had enough of "error: invalid operand" and "error: illegal instruction".

Can anyone give me a clue as to where I should be looking? A few examples of 64-bit integer arithmetic would be wonderful!

EDIT by DOS386 : enhanced subject
Post 31 May 2010, 13:39
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 31 May 2010, 13:52
I think there's a part in the manual where they differentiate GPR and MMX/XMM registers. GPRs are named rax, rbx, ... rbp. MMX registers are mm0..mm7, XMM registers are xmm0..xmm15.

You can access GPR-s through "regular" instructions (mov, add, imul, xor).
You can access MMX/XMM registers through special instructions (starting with P - packed integer instructions; ending with PS/PD - packed float/double instructions).

There are exceptions of course, but that's what manuals are for.

pxor rax is ILLEGAL!

pxor mm0,mm0 or pxor xmm0,xmm0 are okay.
xor rax,rax is okay (although in 64-bit world xor eax,eax does the same thing)
Post 31 May 2010, 13:52
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 31 May 2010, 15:29
john_s,

The best source of information about CPUs are their manufacturers' sites: Intel, AMD.
AMD64 APM vol. 1 rev. 3.15 p. 26 wrote:
3.1.2 64-Bit-Mode Registers

In 64-bit mode, eight new GPRs are added to the eight legacy GPRs, all 16 GPRs are 64 bits wide, and the low bytes of all registers are accessible. Figure 3-3 on page 27 shows the GPRs, flags register, and instruction-pointer register available in 64-bit mode. The GPRs include:
  • Sixteen 8-bit low-byte registers (AL, BL, CL, DL, SIL, DIL, BPL, SPL, R8B, R9B, R10B, R11B, R12B, R13B, R14B, R15B).
  • Four 8-bit high-byte registers (AH, BH, CH, DH), addressable only when no REX prefix is used.
  • Sixteen 16-bit registers (AX, BX, CX, DX, DI, SI, BP, SP, R8W, R9W, R10W, R11W, R12W, R13W, R14W, R15W).
  • Sixteen 32-bit registers (EAX, EBX, ECX, EDX, EDI, ESI, EBP, ESP, R8D, R9D, R10D, R11D, R12D, R13D, R14D, R15D).
  • Sixteen 64-bit registers (RAX, RBX, RCX, RDX, RDI, RSI, RBP, RSP, R8, R9, R10, R11, R12, R13, R14, R15).
Post 31 May 2010, 15:29
View user's profile Send private message Reply with quote
john_s



Joined: 31 May 2010
Posts: 5
Location: UK
john_s 31 May 2010, 15:34
Madis731 wrote:
xor rax,rax is okay (although in 64-bit world xor eax,eax does the same thing)

xor rax, rax
error: illegal instruction.

could there be something wrong with my fasm168?
Post 31 May 2010, 15:34
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 31 May 2010, 15:36
john_s,

Probably you've forgot use64 directive. xor rax, rax has no encoding in non-64-bit modes.
Post 31 May 2010, 15:36
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20513
Location: In your JS exploiting you and your system
revolution 31 May 2010, 15:42
john_s: Make sure you are running a 64bit OS. You can't run 64bit programs on a 32bit OS.
Post 31 May 2010, 15:42
View user's profile Send private message Visit poster's website Reply with quote
john_s



Joined: 31 May 2010
Posts: 5
Location: UK
john_s 31 May 2010, 15:44
Nope - use64 is there.

As a test, I tried removing just about everything else, and it compiled.

I then removed the .data and .code directives, which seemed to be necessary from other examples I've seen, and it seems to be running better. Thanks for the help so far - I'll let y'all know how it goes Smile
Post 31 May 2010, 15:44
View user's profile Send private message Reply with quote
john_s



Joined: 31 May 2010
Posts: 5
Location: UK
john_s 31 May 2010, 15:54
revolution wrote:
john_s: Make sure you are running a 64bit OS. You can't run 64bit programs on a 32bit OS.

I wondered about that. Nothing I've read actually comes out and says it as bluntly as you have. If the registers and instructions are there, why won't it run? I don't plan on calling the operating system at all.

And anyway, shouldn't I be able to compile 64-bit code on a 32-bit OS? Even if it won't actually run?
Post 31 May 2010, 15:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20513
Location: In your JS exploiting you and your system
revolution 31 May 2010, 16:56
john_s: You can compile the 64bit program with fasm on a 32bit system. But you can't run the 64bit program there. You will need another machine to run it with a 64bit OS.

It is not a matter of calling the OS, your program runs under the OS whether you call it or not.

64bit requires the CPU to run in a 64bit mode, you can't just run 64bit code any time. You have to set the CPU mode to 64bit and run everything as 64bit, including the OS.
Post 31 May 2010, 16:56
View user's profile Send private message Visit poster's website Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 31 May 2010, 21:02
Or you could try a VM. I use 32bit xp in vmware under 64bit Linux. I wonder, does vmware emulate all the 32bit code, or do some black magic with compatibility mode?
Quote:

Needing more speed than I can get with high-level languages[...]

You should probably use C. No offence, but you'd have to have a lot of experience before you could write better code than any good compiler, neither I, nor most people can.
Post 31 May 2010, 21:02
View user's profile Send private message Reply with quote
john_s



Joined: 31 May 2010
Posts: 5
Location: UK
john_s 31 May 2010, 22:15
Tyler wrote:
Or you could try a VM. I use 32bit xp in vmware under 64bit Linux. I wonder, does vmware emulate all the 32bit code, or do some black magic with compatibility mode?
Quote:

Needing more speed than I can get with high-level languages[...]

You should probably use C. No offence, but you'd have to have a lot of experience before you could write better code than any good compiler, neither I, nor most people can.

You could well be right.

But the algorithm I'm trying to implement is very simple - using assembler I can keep the whole thing as register-to-register instructions. As far as I'm aware, all high-level languages tend to write values to and from memory, slowing things down by a few orders of magnitude. If I can't get more speed than C then I might as well give up - I'm trying to improve on an existing C implementation.
Post 31 May 2010, 22:15
View user's profile Send private message Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael 02 Jun 2010, 16:52
john_s wrote:
As far as I'm aware, all high-level languages tend to write values to and from memory, slowing things down by a few orders of magnitude.
Compile with enabled optimizations.
Post 02 Jun 2010, 16:52
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 02 Jun 2010, 17:30
You can use the register directive to "suggest" that the compiler use a register for the variable. http://www.itee.uq.edu.au/~comp2303/Leslie_C_ref/C/CONCEPT/storage_class.html#register
Post 02 Jun 2010, 17:30
View user's profile Send private message Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael 03 Jun 2010, 10:56
Tyler wrote:
You can use the register directive to "suggest" that the compiler use a register for the variable.
Good compilers are ignoring that hint, actually.
Post 03 Jun 2010, 10:56
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20513
Location: In your JS exploiting you and your system
revolution 03 Jun 2010, 11:05
Fanael wrote:
Good compilers are ignoring that hint, actually.
Does that mean that "good" compilers always know better than the programmers? If that is the case then I want a "bad" compiler instead.
Post 03 Jun 2010, 11:05
View user's profile Send private message Visit poster's website Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 03 Jun 2010, 13:43
This is why I did away with HLAs :P

_________________
This is a block of text that can be added to posts you make.
Post 03 Jun 2010, 13:43
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 03 Jun 2010, 20:50
mindcooler wrote:

This is why I did away with HLAs Razz

You mean "HLLs"? Either way, almost every popular language has it's use. I'd hate to do anything big in assembly. If I was Tomasz, I'd have went insane years ago, 74.3 kb(Linux Version 1.69.14) of assembled code in assembly would take me 10 life times. Not that I'm saying it's bad, Fasm is the best assembler IMO, it's just that it must have taken a lot of patience.
Post 03 Jun 2010, 20:50
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 03 Jun 2010, 20:56
Yes, I meant HLLs, after all, FASM is an HLA Smile

I managed to reach a whopping 6kb on my first real project in pure assembly.
Post 03 Jun 2010, 20:56
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.