It is currently Tue Oct 17, 2017 7:04 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Questions about x86
PostPosted: Mon Apr 17, 2017 9:35 am 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 294
Location: Colorado USA
So I've decided that I'm going to start going into x86 assembly, and I have quite a few questions. So say that I want to program a game, and of course, I have to have it run in Windows. How would I know what memory addresses to use for variables? I'll have to keep in mind that I'll have to have RAM to store the code, and that Windows will be running in the background as well as a few other apps. You could say that I've been spoiled by NES programming by having the code stored in a ROM as well as having the entire system dedicated to your program and not having to share system resources. I also want to get into programming BIOS' and operating systems, which leads me to even more questions. How do I read and write from a hard disk or CD? And how do I know what memory addresses to use for code and variables? How would I run C code? Could you please use simple terms for your answers? I'm kind of a noob. :roll:


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 9:42 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19091
Location: NE Indiana, USA (NTSC)
Programming an application in assembly language for an operating system is a lot like using ca65 in its intended manner. The operating system loads the code ("text" and "rodata" sections) into RAM when the program starts, and it combines the code in the application with code in shared libraries in a manner similar to a linker. You'd usually list variables in a "bss" section. To read and write files, you make system calls, and a language typically ships with a standard library that adds a more programmer-friendly interface around system calls.


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 9:55 am 
Offline

Joined: Sun Apr 13, 2008 11:12 am
Posts: 6276
Location: Seattle
It's not windows x86, but you might find the Tiny ELF programs for Linux/x86 to be illustrative. (As well as an illustration of How To Go Too Far)


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 9:57 am 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 294
Location: Colorado USA
Could you explain tepples answer in simpler terms?


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 10:01 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19091
Location: NE Indiana, USA (NTSC)
First please read the article "How is a binary executable organized? Let's explore it!" by Julia Evans, and describe the first thing you fail to understand in that article.

The programmer of an application does not specify the address of each individual variable. Instead, he or she specifies the name and size of each variable and their order. The sum of all sizes (after application of alignment directives) gives a value for the total size in bytes of all variables, which is stored inside the executable. A "relocation table", which is a list of all operands in the code that are addresses of variables, is also stored inside the executable.

Then when running the program, the operating system allocates enough memory to hold all of an application's variables. After loading the program from disk into RAM, the operating system reads the relocation table and adds the starting address of the variables to each operand that refers to a variable.


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 10:48 am 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 294
Location: Colorado USA
First, if the programmer doesn't specify which address everything is stored, then how is it stored? Is it with indexed addressing? I don't think that the Intel instruction set has indexed addressing. the artical has a few terms I don't exactly understand, such as function names and ELF binary format. Isn't there only one binary format? Or do they mean stuff like ASCII and other text formats? But if it is a different format, how come it still says "Penguin!"? Maybe it's like when you view an NES ROM in notepad, because all the data is hexadecimal, it looks like complete jibberish because text is stored as hex numbers, so is everything digital. You know what, I just realized that I'm jumping into something I barely have any basic knowledge of, so I'm going to start by doing more basic stuff, because the NES works a whole lot differently then your modern day PC.


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 10:56 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1771
Location: DIGDUG
Virtual Memory, I think.*

And, yes, there is indirect addressing.

*This is what I was thinking of...
https://en.wikipedia.org/wiki/Relocatio ... mputing%29

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Last edited by dougeff on Mon Apr 17, 2017 12:59 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 11:03 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19091
Location: NE Indiana, USA (NTSC)
DementedPurple wrote:
First, if the programmer doesn't specify which address everything is stored, then how is it stored?

The assembler allocates increasing values for the address of each successive variable, relative to the start of variables. The assembler stores in the executable's header the address of every single instruction that reads or writes a variable so that the executable loader can modify each such instruction when loading the program.

Quote:
Isn't there only one binary format? Or do they mean stuff like ASCII and other text formats?

It's like the difference between an iNES header and a UNIF header, or among HTML, a Word document, and a LibreOffice document. 16-bit MS-DOS programs use the MZ header. 32-bit Windows programs and 64-bit Windows programs use the PE header. Linux programs use the ELF header.

Quote:
But if it is a different format, how come it still says "Penguin!"?

Because the character encoding is still ASCII.


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 11:08 am 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 294
Location: Colorado USA
tepples wrote:
The assembler allocates increasing values for the address of each successive variable, relative to the start of variables. The assembler stores in the executable's header the address of every single instruction that reads or writes a variable so that the executable loader can modify each such instruction when loading the program.

So pretty much just indexed addressing, right?


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 11:17 am 
Offline

Joined: Sun Mar 27, 2011 10:49 am
Posts: 191
No; the operating system patches the executable when it loads it and puts the correct addresses in.


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 11:23 am 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 294
Location: Colorado USA
But assume I want to program an operating system, what would I do?


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 11:33 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19091
Location: NE Indiana, USA (NTSC)
Your operating system would read the executable header, see how much space to allocate for variables, allocate that much space, and patch the code to see variables per the instructions in the header.


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 11:45 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1771
Location: DIGDUG
When was the last time someone programmed an OS all by himself? 1970?

I think the base for DOS 1.0 was mostly programmed by 1 man. That was around 1978.

(Tim Paterson, wrote QDOS, which became 86-DOS, which became MS-DOS).

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 11:55 am 
Offline

Joined: Sun Mar 27, 2011 10:49 am
Posts: 191
There are plenty of hobby operating systems in the world. So many that there's an NesDev-like community devoted to it, where your questions on the subject would be more appropriate :)


Top
 Profile  
 
 Post subject: Re: Questions about x86
PostPosted: Mon Apr 17, 2017 12:07 pm 
Offline
User avatar

Joined: Sat Jan 09, 2016 9:21 pm
Posts: 210
Location: Central Illinois, USA
dougeff wrote:
When was the last time someone programmed an OS all by himself? 1970?


TempleOS. It's not very useful, but worth the read.

_________________
My games: http://www.bitethechili.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 5 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group