Live data from Hacker News

X86 assembly doesn’t have to be scary

blog.benjojo.co.uk

51–60 of 129 posts

Re: X86 assembly doesn’t have to be scary

#51
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?

> 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. But when you compare high-end cores, you always see the same picture, regardless of ISA. Large surface area (~1/3 of the core) used for insn fetch/decode/schedule; they look all the same,…

Even 15 decoders are a pinprick on the core’s area—at least when I saw die area for LRB. Fetch & schedule we’re a bit larger. Most of the core area was register files & floating-vector logic.

Re: X86 assembly doesn’t have to be scary

#52
post #38
post #27

Earlier quoted context omitted.

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.

Indeed. Maybe something along the lines of (or building up to) :

"Sshtalk: An SSH-based chat made in assembler" : https://news.ycombinator.com/item?id=15829206

The library comes with a lot, but network stuff and Unicode support to name a few:

https://2ton.com.au/HeavyThing/

Re: X86 assembly doesn’t have to be scary

#54
post #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.

> I don't think any new computer comes booting into 16bit real mode anymore

It most certainly does. See the chapter on 8086 emulation in the Intel Software Developer's Manual, Volume 3.

Re: X86 assembly doesn’t have to be scary

#55
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.

A one-register machine is not the equivalent of a pure stack machine.

Wikipedia has a section on accumulator machines: https://en.wikipedia.org/wiki/Accumulator_(computing)#Accumu...

Assuming Wikipedia's article on the PDP-8 is correct, the PDP-8 had just two public registers: the program counter, and the general-purpose 'accumulator' register.

Re: X86 assembly doesn’t have to be scary

#56
post #51

Earlier quoted context omitted.

> 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. But when you compare high-end cores, you always see the same picture, regardless of ISA. Large surface area (~1/3 of the core) used for insn fetch/decode/schedule; they look all the same,…

Even 15 decoders are a pinprick on the core’s area—at least when I saw die area for LRB. Fetch & schedule we’re a bit larger. Most of the core area was register files & floating-vector logic.

>."..at least when I saw die area for LRB"

What is LRB?

Re: X86 assembly doesn’t have to be scary

#57
post #44
post #42

Earlier quoted context omitted.

But then there's that "msg.len", which I assume is some sort of assembler shortcut for getting the length of an array and not part of the x86.

That's referring to the label msg.len, which is a value computed at compile time in the 'data' section at the bottom. msg: db "Hello, world!", 10 .len: equ $ - msg 'msg' is a label, which will be assigned to an address at compile time. 'db' short for 'declare bytes' puts some bytes at that address. '.len' is then defined as the current output address ($) minus the start of the string.

In addition to all these excellent comments, it can also be instructive to go "backwards" from C, here on Linux:

cat hello.c

  #include 

  char str[] = "Hello, World!\n";

  int main() {
    write(1, str, sizeof(str)-1);
    return 0;
  }
cat hello.s

  .file	"hello.c"
  	.intel_syntax noprefix
  	.globl	str
  	.data
  	.align 8
  	.type	str, @object
  	.size	str, 15
  str:
  	.string	"Hello, World!\n"
  	.text
  	.globl	main
  	.type	main, @function
  main:
  .LFB0:
  	.cfi_startproc
  	push	rbp
  	.cfi_def_cfa_offset 16
  	.cfi_offset 6, -16
  	mov	rbp, rsp
  	.cfi_def_cfa_register 6
  	mov	edx, 14
  	lea	rsi, str[rip]
  	mov	edi, 1
  	call	write@PLT
  	mov	eax, 0
  	pop	rbp
  	.cfi_def_cfa 7, 8
  	ret
  	.cfi_endproc
  .LFE0:
  	.size	main, .-main
  	.ident	"GCC: (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0"
  	.section	.note.GNU-stack,"",@progbits
Note the line: lea rsi, str[rip] which I belive is something along the lines of load effective address (lea), into rsi register, of "str" symbol offset with relative instruction pointer (rip - to allow for relative addressing).

You can compile and run with:

gcc -std=c11 hello.c && ./a.out

Prouduce hello.s from hello.c with: gcc -std=c11 hello.c -S -masm=intel

Re: X86 assembly doesn’t have to be scary

#59
post #57
post #44

Earlier quoted context omitted.

That's referring to the label msg.len, which is a value computed at compile time in the 'data' section at the bottom. msg: db "Hello, world!", 10 .len: equ $ - msg 'msg' is a label, which will be assigned to an address at compile time. 'db' short for 'declare bytes' puts some bytes at that address. '.len' is then defined as the current output address ($) minus the start of the string.

In addition to all these excellent comments, it can also be instructive to go "backwards" from C, here on Linux: cat hello.c #include char str[] = "Hello, World!\n"; int main() { write(1, str, sizeof(str)-1); return 0; } cat hello.s .file "hello.c" .intel_syntax noprefix .globl str .data .align 8 .type str, @object .size str, 15 str: .string "Hello, World!\n" .text .globl main .type main, @function main: .LFB0: .cfi_…

This makes it even easier: https://news.ycombinator.com/edit?id=17341952

Re: X86 assembly doesn’t have to be scary

#60
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?

Intel abandoned it themselves, internally? The whole concept of backwards compatibility to a extensive library of old fixed functions was give up about 10 years gao. All that remains is a bw-compatible Assembly-API and whatever proprietary happens to the microcode generated from this.
Post reply on HN