Live data from Hacker News

X86 assembly doesn’t have to be scary

blog.benjojo.co.uk

31–40 of 129 posts

Re: X86 assembly doesn’t have to be scary

#31
post #14
post #11

I wonder if the history of x86 is holding us back in a big way. It started out being close to the metal but now it's an abstraction that can mislead you if you think processors are literally working the way x86 assembly describes. And surely the whole spectre issue could be lessened if we could be less reliant on CPUs having to guess what to keep in cache, which code paths are most likely, etc?

Well, Intel has tried repeatedly to replace x86 (AXP32, i860/i960, Itanium), but in a way, they were a victim of their own success.

Actually, the iAPX432 came first in 1976. When it became obvious that this project was going to take longer than expected (it was only launched in 1981) Intel did a quick extension to the 8080 to keep customers from going to the competition while they waited.

Re: X86 assembly doesn’t have to be scary

#32
post #11

I wonder if the history of x86 is holding us back in a big way. It started out being close to the metal but now it's an abstraction that can mislead you if you think processors are literally working the way x86 assembly describes. And surely the whole spectre issue could be lessened if we could be less reliant on CPUs having to guess what to keep in cache, which code paths are most likely, etc?

there was at least one non-realized research project where the cache was exposed as part of the architecture to be managed by the compiler (ppc lets you do some of that). i think thats a potentially very fruitful approach, but would it have helped the spectre situation in any way? oh, what you're suggesting in an analog for compiler driven speculation? that may have helped, and is also probably worth thinking about

> but would it have helped the spectre situation in any way?

Presumably a compiler could put cache-lookups and/or cache-waits in non-predictably (perhaps choosing to do so when it suspects a cache-miss anyway).

Re: X86 assembly doesn’t have to be scary

#33
post #24
post #20

When I was about 14 I was enthralled with programming my new Commodore 64, first in BASIC, then 6510 assembly. I had the opportunity to accompany my mother to a one-day class on programming. Being just an intro on the subject, I was well ahead of what they would be discussing, but thought it would interesting to talk to some adults that were also into programming. I was talking to a couple of guys about what I had be…

>"How can you possibly write anything with only three registers?!" AFAIK you can even go down to 1 register, which is how stack machines work. You might even say it’s 0 registers because it’s not something you can directly access.

There are many simple microcontroller architectures with only one register, usually called A (accumulator) or W (work register). You just need to constantly load memory to and from the register, nothing weird in that. Sometimes they will call their memory locations registers, in which case they have lots of registers - the terminology gets quite unclear when everything is on the same chip anyways.

A true stack machine does not have any registers, so that would be 0.

Re: X86 assembly doesn’t have to be scary

#34
post #11

I wonder if the history of x86 is holding us back in a big way. It started out being close to the metal but now it's an abstraction that can mislead you if you think processors are literally working the way x86 assembly describes. And surely the whole spectre issue could be lessened if we could be less reliant on CPUs having to guess what to keep in cache, which code paths are most likely, etc?

>It started out being close to the metal but now it's an abstraction that can mislead you if you think processors are literally working the way x86 assembly describes.

Well, the fact that we were able to abstract it out into an ISA, and still make progress would indicate that it isn't holding us back!

>And surely the whole spectre issue could be lessened if we could be less reliant on CPUs having to guess what to keep in cache, which code paths are most likely, etc?

Why would consumers purchase CPUs that performed worse? The only reason to use branch prediction is because it works and is a huge benefit.

Re: X86 assembly doesn’t have to be scary

#35
post #20

When I was about 14 I was enthralled with programming my new Commodore 64, first in BASIC, then 6510 assembly. I had the opportunity to accompany my mother to a one-day class on programming. Being just an intro on the subject, I was well ahead of what they would be discussing, but thought it would interesting to talk to some adults that were also into programming. I was talking to a couple of guys about what I had be…

Zero-page instructions also helped alleviate the problem of not having many registers.

Re: X86 assembly doesn’t have to be scary

#36
post #20

When I was about 14 I was enthralled with programming my new Commodore 64, first in BASIC, then 6510 assembly. I had the opportunity to accompany my mother to a one-day class on programming. Being just an intro on the subject, I was well ahead of what they would be discussing, but thought it would interesting to talk to some adults that were also into programming. I was talking to a couple of guys about what I had be…

I also started as a programmer on a C64. BASIC didn‘t get you very far, so assembly it was. The machine code monitor, all the three letter mnemonics and ??? when it couldn‘t disassemble are fond memories.

When I switched to the PC I had great hopes but outright hated it after a short while. All this restrictions on the registers were confusing. The segment modell even more. And good graphics required programming the VGA cards bitplane modes [1] which still makes me dizzy when I think about it. Wished I had an Amiga.

Programming the 6510 and VIC and SID was so smooth in comparison.

[1] In these modes the bits making up a single pixel where not in the same place. To manipulate a single pixel you had to manipulate several bits in different places of the RAM without affecting the adjacent bits in the respective words. Crazy stuff.

Re: X86 assembly doesn’t have to be scary

#37
It's hard to find a tutorial that really tells you what is going on. You have to start with whatever operating system you're on, then figure out what the heck you're reading. What is x86 and what is some reserved word peculiar to the assembler you're using?

Here's macOS Hello, world code I see floating around a lot, and I can't make heads or tails of it:

  global start

  section .text
  start:
      push    dword msg.len
      push    dword msg
      push    dword 1
      mov     eax, 4
      sub     esp, 4
      int     0x80
      add     esp, 16

      push    dword 0
      mov     eax, 1
      sub     esp, 12
      int     0x80

  section .data

  msg:    db      "Hello, world!", 10
  .len:   equ     $ - msg

Re: X86 assembly doesn’t have to be scary

#38
post #27
post #3

If your intention is to avoid scaring newbies off, I'm not sure if 16bit real mode, PC boot process, BIOS services and all that arcana that follows is the best place to begin.

I agree with you to an extent: The IBM PC was not a very elegant design, and other computers certainly had less complexity to them. But there is a point I want to make which defends this choice somewhat: Newbies like me are more frustrated by thinking there's no path from introductory material to something useful or realistic. Something that's more immediately friendly would be a simplified virtual machine with no or…

Well, I had in mind something more useful and realistic, not less; namely teaching assembly in normal Linux environment. Sure, there are all sorts of complexities there too, but in general I feel like they are also more worthwhile.

Re: X86 assembly doesn’t have to be scary

#39
What would be more interesting is the UEFI boot process. I don't think any new computer comes booting into 16bit real mode anymore. I would love to see a "Write your own UEFI bootable kernel" I'm sure going straight to a semi sane 32bit environment is much easier to deal with than starting at 16 and working your way up.

Re: X86 assembly doesn’t have to be scary

#40
post #37

It's hard to find a tutorial that really tells you what is going on. You have to start with whatever operating system you're on, then figure out what the heck you're reading. What is x86 and what is some reserved word peculiar to the assembler you're using? Here's macOS Hello, world code I see floating around a lot, and I can't make heads or tails of it: global start section .text start: push dword msg.len push dword…

Basically everything left-aligned is for the assembler, and the indented material is the mnemonics for the x86 set.

The assembler breaks down as:

      push    dword msg.len
      push    dword msg
      push    dword 1
Place a pointer to the message, its length, and 1 on the stack. Each of these decrements esp by 4.

      mov     eax, 4
We'll be doing syscall 4 in a sec

      sub     esp, 4
Reserve 4 bytes for the return

      int     0x80
Do the syscall! See https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/k... ; this is 'write'

      add     esp, 16
Discard the return value and arguments from the stack.

      push    dword 0
      mov     eax, 1
      sub     esp, 12
      int     0x80
Similarly setup and do syscall 1 to exit.
Post reply on HN