Wednesday, July 10, 2024

Why I think the electronics industry is a waste of resources

Why I think the electronics industry is a waste of resources


Growing up with an electrical engineer dad, I was accustomed to seeing PCBs (printed circuit boards) and electronic chips and components. And I loved it, I enjoy electronics and computers very much, which is why I'm a software engineer.

But let's be frank. The electronics industry is a waste of resources. Pen and paper is just fine for everyone.

You know, what happens to the copper on PCBs? They get etched off! They're either lasered off or removed with solution and never seen again. That's copper that they want for EV cars nowadays.

But inevitably, a PCB is more efficient at copper usage than a whole bunch of copper wiring in an ad-hoc wire-wrap circuit.

Of course, I talked about pen and paper before, and writing letters is more inefficient than sending emails, cause it costs petrol to deliver the letters. But CB/Ham radio is more efficient than emails, cause it's just sent over the airwaves, without needing a fibre optic or computer and servers and data centres to run the entire operation.

Morse code telegrams are probably the most resource efficient. They're like CB/Ham radio, but the resources used to create the telegraph machine is cheaper, since all it requires is a coil, however, radio wave interference would reduce its transmission distance, so they would use more electricity.

We need to socialise again. Pen and paper for writing down notes, just like in school, and using our mouths to talk, to give out ideas. They still do this, right?

You know, there's ways to create electronic circuits with conductive ink... imagine drawing a coil on paper, and that acts like an inductor... or two parallel lines, and that actually acts like a capacitor... in fact, circuit diagrams were made for a reason, in that it resembles an actual conductor at work, so conductive ink could be made to work that way.

I want everyone to experience the simpler life. A time where electronics isn't used, and everything can be made from scratch or improvised.

Tennis, anyone? (Wait a second... the machining that it takes to make a racquet, and the rubber used in the tennis ball...)

I know. My new hobby should be gardening. Some of mum's old bamboo shoots are dying/dead, so they need to be replaced.

Drinking tea and coffee. That's my favourite thing to do.

OMG, what is a geek supposed to do if everything is a stupid waste of resources? How do I align myself with the planet? Suggestions anyone?

Wednesday, June 12, 2024

Smallest hello world in linux assembly

Smallest hello world in linux assembly


From https://jameshfisher.com/2018/03/10/linux-assembly-hello-world/


global _start

section .text

_start:
  mov rax, 1        ; write(
  mov rdi, 1        ;   STDOUT_FILENO,
  mov rsi, msg      ;   "Hello, world!\n",
  mov rdx, msglen   ;   sizeof("Hello, world!\n")
  syscall           ; );

  mov rax, 60       ; exit(
  mov rdi, 0        ;   EXIT_SUCCESS
  syscall           ; );

section .rodata
  msg: db "Hello, world!", 10
  msglen: equ $ - msg


However, when we assemble and link it, it will actually be quite large.


$ nasm -f elf64 -o hello.o hello.s
$ ld -o hello hello.o
$ ./hello
Hello, world!

$ ls -al hello
-rwxrwxr-x 1 d d 8872 Jun 12 08:22 hello


That's because it uses a section .rodata, which requires a read-only memory page, on top of a code page (read-executable). So that requires 8KB (if 4KB pages are used) in your binary.


Still, it's smaller than a "puts("Hello world")" C example program.



#include <stdio.h>

int main()
{
    puts("Hello, world!");
    return 0;
}


Size of C program:


$ gcc test.c
$ ls -al a.out
-rwxrwxr-x 1 d d 15960 Jun 12 08:24 a.out

$ strip a.out
$ ls -al a.out
-rwxrwxr-x 1 d d 14472 Jun 12 08:25 a.out



Going back to the assembly program, to shrink it down even more so, the modified source code is then:


global _start

section .text

_start:
  mov rax, 1        ; write(
  mov rdi, 1        ;   STDOUT_FILENO,
  mov rsi, msg      ;   "Hello, world!\n",
  mov rdx, msglen   ;   sizeof("Hello, world!\n")
  syscall           ; );

  mov rax, 60       ; exit(
  mov rdi, 0        ;   EXIT_SUCCESS
  syscall           ; );

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


The same code, without the section .rodata. It just means the text string "Hello World" is actually in the code (.text section) page.

Next step is to strip the binary -- to make it even slightly more smaller.

This is the best we can do, without hacking the binary any further.


$ ls -al hello
-rwxrwxr-x 1 d d 4360 Jun 12 08:19 hello



So that's 4KB, plus abit more, if you include the ELF headers.


Creating our own ELF headers


Taking from the following articles:

https://www.muppetlabs.com/%7Ebreadbox/software/tiny/teensy.html

https://stackoverflow.com/questions/53382589/smallest-executable-program-x86-64-linux


We create our own ELF header and just build using `nasm -o test test.s`


bits 64
            org 0x08048000

ehdr:                                           ; Elf64_Ehdr
            db  0x7F, "ELF", 2, 1, 1, 0         ;   e_ident
    times 8 db  0
            dw  2                               ;   e_type
            dw  62                              ;   e_machine
            dd  1                               ;   e_version
            dq  _start                          ;   e_entry
            dq  phdr - $$                       ;   e_phoff
            dq  0                               ;   e_shoff
            dd  0                               ;   e_flags
            dw  ehdrsize                        ;   e_ehsize
            dw  phdrsize                        ;   e_phentsize
            dw  1                               ;   e_phnum
            dw  0                               ;   e_shentsize
            dw  0                               ;   e_shnum
            dw  0                               ;   e_shstrndx

ehdrsize    equ $ - ehdr

phdr:                                           ; Elf64_Phdr
            dd  1                               ;   p_type
            dd  5                               ;   p_flags
            dq  0                               ;   p_offset
            dq  $$                              ;   p_vaddr
            dq  $$                              ;   p_paddr
            dq  filesize                        ;   p_filesz
            dq  filesize                        ;   p_memsz
            dq  0x1000                          ;   p_align

phdrsize    equ     $ - phdr

_start:
  mov rax, 1        ; write(
  mov rdi, 1        ;   STDOUT_FILENO,
  mov rsi, msg      ;   "Hello, world!\n",
  mov rdx, msglen   ;   sizeof("Hello, world!\n")
  syscall           ; );

  mov rax, 60       ; exit(
  mov rdi, 0        ;   EXIT_SUCCESS
  syscall           ; );

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

filesize      equ     $ - $$


We build it with nasm:


$ nasm -o test test.s

$ ls -al test
-rwxr-xr-x 1 d d 173 Jun 12 08:35 test



The output is 173 bytes! That's small. But it can be improved, based off those articles above.

Why I think the electronics industry is a waste of resources

Why I think the electronics industry is a waste of resources Growing up with an electrical engineer dad, I was accustomed to seeing PCBs (pr...