4

I watched Ben Eater's video about building a computer based on 6502 chip and I'm stuck in part 3 (Assembly language VS Machine code). He is programming 32k EEPROM and he programmed by assembly to blink LED. This is assembler he used http://sun.hasenbraten.de/vasm/ Here his code:

enter image description here

But I have a question about org directive, this is what I understand org tell assembler what address to start right? In picture org equals $8000 so I think first address instruction should be 8000 but when he output file it equal 0000.

Why is the first instruction's address not 8000?

enter image description here

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Heroz
  • 143
  • 2
  • 3
    The `hexdump` command has no way to indicate an origin to it so it assumes zero. The addresses within the code will honour the origin you specified to the assembler however. – ecm Dec 30 '22 at 12:44
  • Followup on ee.SE about how the machine is designed so the first byte of the EEPROM is at CPU address `0x8000`: [What address does it save in EEPROM?](https://electronics.stackexchange.com/q/648405) – Peter Cordes Jan 16 '23 at 11:07

1 Answers1

5

It's a flat binary with no metadata, and hexdump is showing you file offsets anyway, not looking for metadata to figure out load addresses.

If you use xxd to do the hex dumping, it has a -o option that lets you specify an offset to add to the file position. xxd -g 1 -o 0x8000 a.out should start at 00008000 and generally resemble hexdump -C (1-byte groups, -groupsize 1)

Disassemblers for flat binaries typically have similar options, to disassemble as if the image was loaded / mapped to a certain base address in memory.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • thank you. Does that mean it will save at address 8000 in EEPROM ? – Heroz Dec 31 '22 at 14:40
  • @Heroz: If you wire the EEPROM up to respond to addresses starting at 8000, then yes. (Or if the EEPROM is wired up so its internal address 0 is the machines address 0, then yes if you program the EEPROM to have this file starting at that address.) As I said, the assembler just makes a flat binary file with no metadata. It's up to you (or Ben Eater) to take care of putting those bytes in the right place and wiring up the hardware correctly so the actual run-time address will match the `org` you told the assembler to assume when it was turning source into binary. – Peter Cordes Dec 31 '22 at 14:47
  • 1
    sorry but his EEPROM is 32k and its last address is 7fff how he can save at address 8000? – Heroz Dec 31 '22 at 15:06
  • @Heroz: 32 K *bits*, so only 8K addresses? Then you can't, he must be wiring up the 8KiB EEPROM so its address 0 is the machine's address 8000. (Like decoding the high address bits into a chip-select signal for it, or however this machine works. I haven't watched Ben Eater's videos and don't know the details of the design of this system.) – Peter Cordes Dec 31 '22 at 15:11
  • yes , he did that . But why he need to tell assembler to start at 8000 when he decode the high address bits into a chip-select signal . – Heroz Dec 31 '22 at 15:28
  • @Heroz: So if you write code like `foo:` `lda foo` where the assembler needs to turn a symbol into an absolute address, it will know the right absolute address to invent. The code in the file you should is only using numeric addresses so you'd get the same binary output file with or without `org` (try it yourself; check with `diff` or `cmp`, or `md5sum`; there is no magic here, the bytes in the file will get programmed into the EEPROM, source code only affects the final computer via differences in this file.) – Peter Cordes Dec 31 '22 at 15:33
  • Sorry to ask you many questions but what happen when programmed .org 8000 how code was saved in 0000? – Heroz Dec 31 '22 at 16:00
  • 1
    @Heroz: IDK what you're talking about. If you mean offset zero of the EEPROM's address, that's done by giving the binary file to the EEPROM programmer. The `8000` only comes up in address decoding by hardware while running, and software assumptions while assembling. – Peter Cordes Dec 31 '22 at 16:03
  • This is what I mean he programmed org 8000 and I think code will save at 8000 in EEPROM actual address but EEPROM is only 0-7fff so what EEPROM address did it save at ? – Heroz Jan 01 '23 at 12:41
  • @Heroz: Offset `0` of the file is at address 0 in the EEPROM's address `0`, which is wired up to respond when the CPU asks for address `8000`. In other words, the 8KiB `.bin` file is an image of the EEPROM contents. – Peter Cordes Jan 01 '23 at 13:07
  • Correction, 0x8000 bytes is 32KiB. `0x1000` is 4KiB; I was mixed up about the EEPROM size earlier. – Peter Cordes Jan 03 '23 at 04:39
  • `org` statements don't exist once the source code is assembled into an executable. It's not going to be anywhere in the hexdump. `org` exists so that your `JMP` and `JSR` statements have the correct operands when using labels, and to ensure proper setup of banks and the vector table. – puppydrum64 Jan 05 '23 at 11:39