News for hackers. Stuff that matters.
Re:AsmBB – a lightweight web forum engine written in assembl... by andoando on Saturday January 13, 2024 @03:20PM (#38984139)
Man how do you even connect to a db with assembly code? Are there assembly libraries?

In theory I understand it but the monumental amount of effort it would take to write such “simple” things would take so much time

Re:AsmBB – a lightweight web forum engine written in assembl... by atdt on Saturday January 13, 2024 @03:29PM (#38984213)
It's less complicated than you might think. You call into SQLite C library functions using C calling conventions. Here's the relevant code: https://asm32.info/fossil/asmbb/file?name=source/sqlite3.asm...
Re:AsmBB – a lightweight web forum engine written in assembl... by andoando on Saturday January 13, 2024 @03:51PM (#38984426)
How about for setting up a server and serving http requests / establishing web sockets?

If it’s just a bunch of calling C code then I have to ask which part of this is actually assembly

Re:AsmBB – a lightweight web forum engine written in assembl... by wruza on Saturday January 13, 2024 @03:59PM (#38984494)
You can ask the same about e.g. Pascal, Python and Javascript, because all of them just make a bunch of C calls at the end of the day and can’t use NIC ports directly.
Re:AsmBB – a lightweight web forum engine written in assembl... by wavemode on Saturday January 13, 2024 @03:55PM (#38984461)
To be fair, from that perspective every backend web application in every language is really just "calling C code".
Re:AsmBB – a lightweight web forum engine written in assembl... by eterm on Saturday January 13, 2024 @05:43PM (#38985399)
That's overly simplistic and untrue. If you write a C# / .Net program you're not "just calling C code", you're not calling C at all.
Re:AsmBB – a lightweight web forum engine written in assembl... by tsm212 on Saturday January 13, 2024 @08:03PM (#38986377)
But the CLR is written c, c++ and assembly which is what runs c#. Also the syscalls that run down the chain are also written in c, c++ etc.
Re:AsmBB – a lightweight web forum engine written in assembl... by struant on Sunday January 14, 2024 @08:49AM (#38990333)
That is just the runtime. That is smaller of fraction of what is going on when your programs execute than you think.

The C# code you write (and majority of the your code's dependencies) are JIT compiled to native machine code (or more recently there are AOT compilation options).

Re:AsmBB – a lightweight web forum engine written in assembl... by mldbk on Saturday January 13, 2024 @06:40PM (#38985880)
but then your runtime VM calls C :)
Re:AsmBB – a lightweight web forum engine written in assembl... by duped on Saturday January 13, 2024 @06:03PM (#38985587)
To do this in assembler you write raw syscalls to bind to open the TCP stream and then accept to handle incoming connections, possibly with fork or clone3 to spawn a process/thread to handle the new connection. None of that requires C programming. HTTP is "just" some text parsing on top of read/write calls to the file descriptor returned by accept. Web sockets are "just" some connection logic on top of HTTP.

A basic implementation of that is totally possible in assembly with no calls to c libraries. Would even be a great project for people to learn some systems programming. Keep in mind, all of these protocols (except maybe web sockets) were designed when programming in assembler was common. There's probably some HTTP implementation in the wild doing the same thing.

That said there's a lot of reasons you probably don't want to do that, HTTP can be subtle and you probably don't want to be doing raw clone3 calls to spawn threads, if you want to be doing thread or process per connection in the first place.

Re:AsmBB – a lightweight web forum engine written in assembl... by deaddodo on Saturday January 13, 2024 @06:56PM (#38985999)
Well, you see, most OSes today aren't written in ASM. Their native libraries are C, so you have to interface with those libraries if you want to utilize them and their features.

You can certainly write your own TCP/IP stack with raw sockets and directly interface with the NIC, if you like. But at that point you're both reinventing the wheel and bypassing the entirety of the OS, you might as well just write a forum exokernel.

Most developers don't consider utilizing a library in another language as "cheating". Popular and core Rust, D, C++, Python, nodeJS, etc libraries do this all the time.

Re:AsmBB – a lightweight web forum engine written in assembl... by pengaru on Saturday January 13, 2024 @04:09PM (#38984596)
C is generally first compiled into assembly anyways.

When you program in assembly you're basically just doing the job of the compiler, and can call all the same library functions C code can. You just have to follow the calling conventions of the architecture. It's cumbersome and tedious, but nowhere near impossible.

x64 made calling functions more annoying by using registers then spilling over into the stack after exhausting those. x32 was more pleasant by far, since it only used the stack. x64's convention produces faster code though, by keeping stuff in registers.

Re:AsmBB – a lightweight web forum engine written in assembl... by nineteen999 on Saturday January 13, 2024 @05:33PM (#38985319)
> x32 was more pleasant by far, since it only used the stack

Maybe I misunderstand you, but this is completely dependent on compiler, operating system etc, there are multiple calling conventions for x86-32, eg. cdecl, fastcall ...

https://en.wikipedia.org/wiki/X86_calling_conventions

Re:AsmBB – a lightweight web forum engine written in assembl... by pengaru on Saturday January 13, 2024 @07:20PM (#38986121)
Apologies, my experience is SysV-centric for amd64, as described here[0] under the Parameter Passing section, which is a clustrfuck compared to the simplicity of the 386 SysV ABI as described here[1]:

  > Argument words are pushed onto the stack in reverse order (that is, the
  > rightmost argument in C call syntax has the highest address), preserving the
  > stack’s word alignment. All incoming arguments appear on the stack, resid-
  > ing in the stack frame of the caller.
Both links were found @ https://wiki.osdev.org/System_V_ABI

[0] https://www.uclibc.org/docs/psABI-x86_64.pdf

[1] https://www.sco.com/developers/devspecs/abi386-4.pdf

Re:AsmBB – a lightweight web forum engine written in assembl... by kristianp on Saturday January 13, 2024 @05:37PM (#38985352)
> C is generally first compiled into assembly anyways.

I don't think that's right. C is compiled into an intermediate representation first. It's true that you can ask GCC etc. to generate assembly output, but it's not the default.

Re:AsmBB – a lightweight web forum engine written in assembl... by pengaru on Saturday January 13, 2024 @07:08PM (#38986066)
> I don't think that's right. C is compiled into an intermediate representation first. It's true that you can ask GCC etc. to generate assembly output, but it's not the default.

Just because there's an IR within the compiler doesn't mean there's no "assembly" stage. What do you think the "-pipe" flag to gcc is for? It pipes the compiler output into the assembler, rather than using a temporary .s file.

gcc has four main stages going from source to executable: preprocessor->compiler->assembler->linker.

Edit, for your convenience, gcc -save-temps example:

  > [user@host /tmp/foo]$ ls
  > f.c
  > [user@host /tmp/foo]$ cat f.c
  > #include <stdio.h>
  >
  > int main(int argc, char **argv)
  > {
  >         puts("Hello world!");
  >
  >         return 0;
  > }
  > [user@host /tmp/foo]$ gcc -o f -save-temps f.c
  > [user@host /tmp/foo]$ ls -l
  > total 48
  > -rwxr-xr-x 1 user user 15416 Jan 13 16:50 f
  > -rw-r--r-- 1 user user    91 Jan 13 16:49 f.c 
  > -rw-r--r-- 1 user user 17145 Jan 13 16:50 f.i 
  > -rw-r--r-- 1 user user  1496 Jan 13 16:50 f.o 
  > -rw-r--r-- 1 user user   515 Jan 13 16:50 f.s 
  > [user@host /tmp/foo]$ cat f.s
  >         .file   "f.c"
  >         .text
  >         .section        .rodata
  > .LC0:
  >         .string "Hello world!"
  >         .text
  >         .globl  main
  >         .type   main, @function
  > main:
  > .LFB0:
  >         .cfi_startproc
  >         pushq   %rbp
  >         .cfi_def_cfa_offset 16
  >         .cfi_offset 6, -16
  >         movq    %rsp, %rbp
  >         .cfi_def_cfa_register 6
  >         subq    $16, %rsp
  >         movl    %edi, -4(%rbp)
  >         movq    %rsi, -16(%rbp)
  >         leaq    .LC0(%rip), %rax
  >         movq    %rax, %rdi
  >         call    puts@PLT
  >         movl    $0, %eax
  >         leave
  >         .cfi_def_cfa 7, 8
  >         ret
  >         .cfi_endproc
  > .LFE0:
  >         .size   main, .-main
  >         .ident  "GCC: (GNU) 13.1.1 20230429"
  >         .section        .note.GNU-stack,"",@progbits
  > [user@host /tmp/foo]$ ./f
  > Hello world!
  > [user@host /tmp/foo]$ 
  >
Re:AsmBB – a lightweight web forum engine written in assembl... by kristianp on Sunday January 14, 2024 @04:41PM (#38994707)
Thanks for the correction, I assumed the assembly stage was not done.
Re:AsmBB – a lightweight web forum engine written in assembl... by kragen on Saturday January 13, 2024 @08:57PM (#38986660)
there are compilers that do not generate textual assembly output from their ir, but gcc in particular does
Re:AsmBB – a lightweight web forum engine written in assembl... by kragen on Saturday January 13, 2024 @08:56PM (#38986651)
i suspect you mean i386, not x32; x32 is an almost unused abi added in 02011 for 64-bit amd and compatible processors, available with gcc -mx32. https://lwn.net/Articles/548838/ talks a bit about its early adoption

it's so little used that i can't find the abi documentation, but gcc -c -mx32 does seem to generate code that passes arguments in registers, not on the stack, contrary to your assertion

edit: after enough digging, i found https://web.archive.org/web/20130902094821/https://sites.goo... linking to https://web.archive.org/web/20201015005434/https://3263a7b2-... but x32-abi-1.0.pdf seems to be missing from the archive and https://web.archive.org/web/*/https://sites.google.com/site/...* doesn't look at all promising

Re:AsmBB – a lightweight web forum engine written in assembl... by pengaru on Saturday January 13, 2024 @09:00PM (#38986681)
You're right, I was sloppy in typing x32 for i386 (as you mentioned, nobody actually uses that).
Re:AsmBB – a lightweight web forum engine written in assembl... by kragen on Saturday January 13, 2024 @09:02PM (#38986683)
cool, thanks for clarifying
Re:AsmBB – a lightweight web forum engine written in assembl... by quesera on Saturday January 13, 2024 @09:38PM (#38986889)
> C is generally first compiled into assembly anyways.

Splitting hairs perhaps, but the C compiler output is machine code, right? No reason to generate assembly along the way.

Re:AsmBB – a lightweight web forum engine written in assembl... by pengaru on Saturday January 13, 2024 @10:09PM (#38987049)
> > C is generally first compiled into assembly anyways.

> Splitting hairs

> > generally

smh

https://en.wikipedia.org/wiki/Compiler#Three-stage_compiler_...

Re:AsmBB – a lightweight web forum engine written in assembl... by nneonneo on Saturday January 13, 2024 @03:25PM (#38984183)
It says that the DB is SQLite, so I presume they’re just linking to the SQLite libraries.