flat assembler
Message board for the users of flat assembler.

Index > Main > Indexing data with a base address

Author
Thread Post new topic Reply to topic
Logos



Joined: 23 Sep 2015
Posts: 13
Logos
I'm new to x86 assembler, and have a basic question.

What is the equivalent to the following 6502 assembler instructions?

LDX #3
LDA $1000,X ; Load the contents of address $1000 + 3 = $1003

...and...

LDX #3
STA $1000,X ; Store data in address $1000 + 3 = $1003
Post 25 Sep 2015, 01:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
In 32-bit code you can use any register as a base, or an index, or both. The index can be multiplied by 1,2,4 or 8. And an offset can be included also.
Code:
mov eax,[ebx + ecx*4 + 0x1000]    


Last edited by revolution on 25 Sep 2015, 02:16; edited 1 time in total
Post 25 Sep 2015, 01:42
View user's profile Send private message Visit poster's website Reply with quote
Logos



Joined: 23 Sep 2015
Posts: 13
Logos
Thanks! That was very informative.

Looks like x86 assembler is a bit like a black art.

When I have searched for information on x86 assembler on the internet, I have never found any information like that.

Where could I have found that information? Is there any web page or document?
Post 25 Sep 2015, 02:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
Both Intel and AMD provide the CPU instruction set references on their respective websites. Those are the canonical references. But even the fasm download has the instructions explained in the fasm.pdf file. And hundreds of other websites have the CPU instruction set(s) explained in various ways and to various levels of detail and precision.
Post 25 Sep 2015, 02:16
View user's profile Send private message Visit poster's website Reply with quote
Logos



Joined: 23 Sep 2015
Posts: 13
Logos
Is the static offset a signed 32bit value?

I have looked at many different sites, and never seen any example on using two registers combined for an address. And I have not seen any site talking about that you can use any register as a address.

To me it looks like, most sites refer to very old information on how to program in assembler.

Where can I find the fasm.pdf file? I Looked for it, but could not find any file named like that. But I finally found it. It's only in the 'flat assembler 1.71.39 for Windows' archive, not in the Linux, dos, or Unix archives on the download page.

And it's not talked about in the fasm.pdf file. The only thing I can find about it, is the following somewhat misleading quotes from that file...

Quote:

In case when address is not relative to any registers, those
operators allow also to choose the appropriate mode of absolute addressing.


It's also stated in that document that...
Quote:

The mov can be assembled only if the size of source operand and size of destination operand are the same. Below are the examples for each of the allowed
combinations:

...followed by examples. But none of them talks about combining register, or adding a static offset.
Post 25 Sep 2015, 17:49
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
You probably read the old DOS tutorials, that target the first 8088/8086 processors in real mode. The assembly language for 32/64 bit protected mode is much more powerful and flexible.

Anyway, download and read the Inter reference. As revolution said, It is the canonical reference. Although it is a long read - several thousands pages.

Also, try to search explicitly assembly for Windows/Linux programming tutorials. They describe exactly what you need.

Also, try to read the example programs in the FASM package and the code posted in this forum.
Post 25 Sep 2015, 18:12
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 204
ACP
This a bit old but not bad introduction to x64 architecture: http://www.cs.cmu.edu/~fp/courses/15213-s07/misc/asm64-handout.pdf

It is shorter than Intel manuals so it may be good starting point.
Post 26 Sep 2015, 08:17
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7801
Location: Kraków, Poland
Tomasz Grysztar
ACP: I'm not sure whether pointing to a document that uses the gas syntax is a good idea here.

To OP: fasm's manual itself recommends reading the Intel Architecture Software Developer's Manual, because fasm's documentation contains only cursory information about the x86 architecture - it focuses on defining the syntax options for every instruction, because this is the information that may be specific to an assembler (some syntax options vary between the different assemblers), while it assumes that the programmer has the basic knowledge of the architecture from elsewhere (like said Intel manual). About the addresses it only says in section 1.2.1 that "the address (also any numerical expression, but it may contain registers) should be enclosed in square brackets or preceded by ptr operator". For details on what kind of numerical expression containing register is allowed you need the knowledge about the specific processor architecture (and the official manual from Intel is the best source of it). Note that fasm may allow expressions containing registers to be more complex that the ones defined by architecture, as long as they are algebraically equivalent, for example "inc byte [(eax+2)*3]" is going to be assembled into "inc byte [eax+eax*2+6]".

Because the Intel manuals in their current iteration have grown into thousands of pages that may be overwhelming for a beginner (especially when a knowledge about the instruction sets like MMX/SSE, x86-64 or AVX may not be needed for someone that just want to start with basic x86 programming), I would recommend reading the old 80386 manual from 1986. This is the document that I used myself when writing the very first version of fasm, and it contains a lot of the useful information, while not being as overwhelming as the modern ones. You can find the addressing through base, index and displacement explained nicely in section 2.5.3.2, "Effective-Address Computation".
Post 26 Sep 2015, 12:48
View user's profile Send private message Visit poster's website Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 204
ACP
Tomasz Grysztar wrote:
ACP: I'm not sure whether pointing to a document that uses the gas syntax is a good idea here.


Fair point but since the discussion was about the instruction set not FASM itself I allowed myself to post the link as I find the document useful even if example code is using different syntax. There are many other publications that are - unfortunately - using gas instead of FASM syntax so I guess there is nothing wrong when newbies discover it sooner than later.
Post 26 Sep 2015, 14:41
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Well, the children, as a rule, know about the pornography, but this does not means that the parents should pointing them to the best porno sites. Are they? Very Happy
Post 26 Sep 2015, 16:08
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 204
ACP
JohnFound wrote:
Well, the children, as a rule, know about the pornography, but this does not means that the parents should pointing them to the best porno sites. Are they? Very Happy


I don't thing your example apply here. There are many assemblers and while we probably all agree that FASM is superior you still can find useful code using different syntax. Learning to read code in a different syntax is useful and part of education process while pointing porn sites isn't. Secondly I will be more than happy to read similar - to the one I've pointed in good faith - FASM-centric paper. I'm not aware of such but I'm sorry if I'm wrong and I will be more than glad to read it.
Post 26 Sep 2015, 18:13
View user's profile Send private message Reply with quote
Logos



Joined: 23 Sep 2015
Posts: 13
Logos
I think that it would be appropriate to add a few more lines, in the examples in the "2.1.1 Data movement instructions" in the fasm manual, that demonstrate Based-Indexed Addressing (for use in the source and destination) not just indirect addressing.

A few more lines in that document can't hurt. And certainly not than almost all information about x86 programming is ignorant about it.

It would be a great improvement.

I tried to find older original documents, but couldn't find any on intels site or elsewhere. So thanks for the following statement:

Quote:
I would recommend reading the old 80386 manual http://board.flatassembler.net/topic.php?p=55709#55709 from 1986. This is the document that I used myself when writing the very first version of fasm, and it contains a lot of the useful information, while not being as overwhelming as the modern ones. You can find the addressing through base, index and displacement explained nicely in section 2.5.3.2, "Effective-Address Computation".


A few more lines in the fasm manual would spare future x86 explorers a huge amount of time. People have a short attention span, and it's a huge wast of time to spend hours and days for each individual, just to get hold of basic information that only could have taken a few minutes if it was pointed out from the start. So a few lines in the appropriate place could spare many hundreds hours in total.

Remains of the following:
Quote:
Larry Kenyon was the engineer working on the disk driver and file system. Steve came into his cubicle and started to exhort him. "The Macintosh boots too slowly. You've got to make it faster!"

Larry started to explain about some of the places where he thought that he could improve things, but Steve wasn't interested. He continued, "You know, I've been thinking about it. How many people are going to be using the Macintosh? A million? No, more than that. In a few years, I bet five million people will be booting up their Macintoshes at least once a day."

"Well, let's say you can shave 10 seconds off of the boot time. Multiply that by five million users and thats 50 million seconds, every single day. Over a year, that's probably dozens of lifetimes. So if you make it boot ten seconds faster, you've saved a dozen lives. That's really worth it, don't you think?"
Post 26 Sep 2015, 22:08
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie
Logos wrote:
I think that it would be appropriate to add a few more lines, in the examples in the "2.1.1 Data movement instructions" in the fasm manual, that demonstrate Based-Indexed Addressing (for use in the source and destination) not just indirect addressing.

A few more lines in that document can't hurt. And certainly not than almost all information about x86 programming is ignorant about it.

It would be a great improvement.


You probably got the wrong idea of what FASM manual is all about. FASM manual isn't a textbook on Assembly Programming that teaches all kind of addressing modes. Even the official intel manual mentions only 3 addressing modes while other textbooks mention 7.
Post 27 Sep 2015, 14:10
View user's profile Send private message Visit poster's website Reply with quote
Logos



Joined: 23 Sep 2015
Posts: 13
Logos
fasmnewbie wrote:

You probably got the wrong idea of what FASM manual is all about. FASM manual isn't a textbook on Assembly Programming that teaches all kind of addressing modes. Even the official intel manual mentions only 3 addressing modes while other textbooks mention 7.


The x86 need better documentation, so why would 3-4 more lines make the universe worse of?

What is the reason why most information about x86 assembler, still talks about how to programs like we still live before 1996? In 1996 you had both windows 95 and you had direct X, so you could make programs and games that run faster in windows than in DOS (Win 95 used a flat memory model).

And the x86, has the flat memory model in protected mode since 1986, when the 386 was introduced. But people still talks about how to program x86 processors, in ways that in best of worlds, should have been forgotten when we left the 80's.

In that context, it would be great if people stopped to program like it still was 1984.

It's not a good thing if people stay ignorant about x86 assembler. Why do most programmers ignore x86 assembler? I don't think it's about the fantastic speed or the orthogonal instruction sets of the x86, that all modern OS use. It's about the huge amount of misleading and confusing documentation, that hides the processors true nature. And talks about it like it had no orthogonality at all, and like it has really crippled addressing modes and instruction set, compared to a RISC processor.

Most x86 documentation is like taking a acid trip, as it talks about how to program like it still was 1984, and talks about huge amount of unused modes of the x86, mashed some crippled sprinkles of 386 programming in the flat memory model.

Documentation about x86 is confusing, as it talks a lot about possible programming models, that no one uses. And they teaches how to program like the x86 has no orthogonal instructions.

As I learn more about the x86 processor, I like it more and more. But there is a huge amount of misleading information out there. In actuality, x86 is more like a really good RISC processor to program. But to get there, you have to ignore 95% of all you read about this processor.

There is a really good reason why x86 processor is that fast. And it's because it has less registers than a modern RISC processor. Many registers makes RISC processors slow to use for normal computer use.

Take the Power PC processor, it has a huge amount of registers. This makes it fast for DSP applications. But in a OS with lots of applications running, it uses a huge amount of time wasted to save and reload all registers when switching threads.

One of the things that make ARM good for running a common OS with lots of applications, is that it has less registers than most RISC processors.

I really begin to like the x86 processor, but it has nothing to do with the documentation. But the fasm documentation is much better than most information out there.
Post 27 Sep 2015, 17:53
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie
PEexecutable]

I really don't know what you want after reading your elaborated comment. Just like I said, Intel Manual is not a textbook on x86 programming simply because, we earthlings been told that Intel's INSTRUCTION MNEMONICS ARE NOT A PROGRAMMING LANGUAGE. Intel provides definitions via symbolic interfaces so that they don't have to use binaries to explain what the CPU does. They especially don't teach you how to program. It was the third party initiatives such as Tomasz that turn the mnemonics into actual implementation by providing a compiler so that we can all use an assembler. Tomasz can change MOV instruction mnemonic to MOVE_IT_BABY and implement it like a MOV. That's when you need an assembler manual, to explain such mnemonic implementation and definition process. That's why we can see similar instruction set references in FASM manual because Tomasz wants us to know how he implements such thing.
Post 28 Sep 2015, 04:19
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
fasmnewbie wrote:
PEexecutable
Erm, I don't see where any comment was made in this thread by PEexecutable. Confused
Post 28 Sep 2015, 04:22
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie
revolution wrote:
fasmnewbie wrote:
PEexecutable
Erm, I don't see where any comment was made in this thread by PEexecutable. Confused
That's because we are lovers. Lovers know each other's smell.
Post 28 Sep 2015, 04:26
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
fasmnewbie wrote:
That's because we are lovers. Lovers know each other's smell.
I think I didn't want to know that. TMI Confused Confused
Post 28 Sep 2015, 04:36
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.