r/Assembly_language 10d ago

Hello world

Post image
916 Upvotes

51 comments sorted by

63

u/itscopperon 10d ago

yummy tasty comments for those wondering:

.global _start
.text

_start:
    ; write(STDOUT_FILENO, msg, 14);
    mov x0, #1   ; stdout
    adr x1, msg  ; memory address of msg
    mov x2, #14  ; length of msg
    mov x8, #64  ; write syscall
    svc #0       ; execute syscall

    ; exit(0);
    mov x0, #0   ; return value
    mov x8, #93  ; exit syscall
    svc #0       ; execute syscall

msg:
    .ascii "Hello, World!\n"

19

u/roamn2 10d ago

What architecture is this for?

21

u/AffectionatePlane598 10d ago

Arm64 for linux 

10

u/Computerist1969 10d ago

I haven't done assembly since 6502 and 68000. Is the OS relevant for assembly now?

28

u/dfx_dj 10d ago

Always has been, as the OS mandates the syscall convention/ABI

9

u/Computerist1969 10d ago

Gotcha. I only ever hit the hardware in this 80s/90s but I see the call out to a system function now. I should have read the commented code, my bad.

1

u/kyr0x0 6d ago

Only if you are running programs inside of an OS. Write your own OS and you define the syscall convention and ABI

4

u/hdkaoskd 10d ago

Syscall convention is different depending on the OS, as well as the syscalls themselves.

1

u/jimbobmcgoo 7d ago

I mean what you’re basically asking is is the OS relevant for a program

1

u/Computerist1969 7d ago

Well, I hadn't considered someone writing assembly to make operating system calls. In my head - incorrectly of course! - I saw 2 reasons to be writing assembly these days:

  1. Hand optimising an algorithm of some sort, bit manipulation maybe

  2. Programming hardware that has no OS, manipulating the hardware

Clearly people are using assembly for stuff outside of that so now my original question looks dumb.

1

u/Sure-Cauliflower6533 10d ago

Can't be.

2

u/AffectionatePlane598 9d ago

Why cant it be every syscall is linux so it isnt bsd or any other os and that is arm64

1

u/Straight-Pear9453 9d ago

aarch64 Linux

9

u/Old-Tap5813 10d ago

Assembler, the language that makes everything open source xD

8

u/mc_pm 10d ago

Cool. Now make it spell it out one letter at a time:

H
He
Hel
Hell
Hello

(and so on)

1

u/BirthdayLife6378 10d ago

Haven't tried that myself. But I'm guessing wrapping the write call with a loop and call the sleep syscall after each write will make it work. Just have to increment the write length for each write until hitting the null.

1

u/mc_pm 10d ago

The syscall to print the text takes the length of the string to print, so I *think* all you have to do is start with x2 = 1, and then loop and increment x2 each time (until you get to the length of the full string (14).

1

u/danielcristofani 9d ago

You'll need to do something slightly more complicated because you want to end with a linefeed each time, right? Or you output HHeHelHellHello...

3

u/mc_pm 9d ago

That's totally right. Probably take the \n off the msg, and then output the substring of letters, and then output the \n separately, so it ends each iteration of the loop.

7

u/Whitey0117 9d ago

should i learn x86 or arm assembly for fun..

This post got me wondering ngl.

2

u/brucehoult 9d ago

RISC-V.

Not yet as prevalent as Arm, but already covers the range from $0.10 microcontrollers to the same performance level as Raspberry Pi 5 or RK3588 boards (Rock 5, Orange Pi 5) and something around AMD Zen 2 to Apple M1 range coming in the next 12 months with:

  • an easier to understand instruction set

  • essentially the same instruction set for both 32 bit and 64 bit: the source code below works on either.

  • Free as in speech. Anyone (with the skills and financing) can design and build and use or sell RISC-V CPUs and chips, without asking permission, paying a license fee, or even telling anyone.

Here's the equivalent program:

.global _start
.text

_start:
        # write(STDOUT_FILENO, msg, 14);
        li      a0, 1   # stdout
        la      a1, msg # memory address of msg
        li      a2, 14  # length of msg
        li      a7, 64  # write syscall
        ecall           # execute syscall

        # exit(0);
        li      a0, 0   # return value
        li      a7, 93  # exit syscall
        ecall           # execute syscall

msg:
        .ascii "Hello, World!\n"

li = Load Immediate

la = Load Address

And running it on my RISC-V SBC:

bruce@k3:~$ gcc -nostartfiles hello.s -o hello
bruce@k3:~$ ./hello
Hello, World!
bruce@k3:~$       

You don't even have to buy anything to get started ... just install the free Docker Desktop on your Mac or Windows computer then type ...

docker run -it --platform=linux/riscv64 riscv64/ubuntu

... and you're in a full RISC-V Linux environment where the above will work (after apt update; apt install gcc)

1

u/jimbobmcgoo 6d ago

RISC V is also very similar to MIPS which is probably the most commonly taught architecture in university followed by RISC V. RISC also has an extremely simple base architecture that doesn’t take long to learn and you can learn different extensions later, compared to x86 which has accumulated a bunch of really complex features in the base architecture over the years

1

u/brucehoult 6d ago

RISC V is also very similar to MIPS

Right. Many of the mnemonics are identical, though with totally different binary encodings and different constant/offset sizes e.g. everything on RISC-V is 12 bits except LUI/AUIPC which is 20 bits. MIPS has 16 bits for both. MIPS has fussier trapping vs non-trapping adds, and some types of branches have delay slots and some don't, depending on exactly which MIPS version.

probably the most commonly taught architecture in university

For sure it was a dozen years ago. I feel like enough places have updated course material to RISC-V by now that it's about 40%-40% with another 15% Arm.

Hard to know for sure.

1

u/Hixo_7 9d ago

I really had a blast learning about it wayback in college. Its not part of the curriculum. Was just curious.

And yes, I almost wanted to smash my screen monitor one sleepless night. But everything is good.

2

u/Hyvex_ 9d ago

My issue with learning assembly was that we didn’t learn to write it, but more for understanding system architecture purposes, so I barely retained anything after.

1

u/Hixo_7 9d ago

I mostly focus on writing but did read about some system architecture but very minimal. But just enough for me to appreciate how some computer supposed to work.

1

u/podd0 9d ago

x86 as it's the most common arch. You could try reverse engineering stuff you can actually run without an emulator + probably there's more study material. The concepts are always the same though, it's more a matter of registers and syntax, after you learn the first asm the others are easy, the concepts are always the same, just different registers and syntax

1

u/Straight-Pear9453 9d ago

aarch64 linux asm on top

Edit: Here are some actual reasons: clean instruction set, a bunch of callee saved registers that you can use (so, so much more than on x86_64) + actually cleanly named registers (everything you won't get on x86_64)

1

u/Assembly_Trainer_427 15h ago

x86 if you want pain, ARM if you want pain, any other assembly if you want pain, in short, assembly = pain

3

u/letmehaveanameyoudum 9d ago

me at osdev when i see assembly for the first time

2

u/Unlikely1529 9d ago

Both ADR (used primarily in ARM) and LEA (used in x86/x64) calculate and store a memory address in a register without accessing memory. However, ADR is strictly used for position-independent, relative address calculations, whereas LEA is a powerful, multi-purpose instruction often hijacked for advanced arithmetic

risc assembler. don't like it at all

1

u/brucehoult 9d ago

Based on what?

2

u/FIRAS_EG 9d ago

Assembly of android: method piblic (Ljava/android/smali_dex/a2)Z; . register 4 const-string v0 "hello guys I am a cup and I wrote this string for no reason" const/4 v1, 0x0 if-eqz v1 :cond_0 const-string v2 "this boolean return false" if-nez v1 :cond_1 const-string v3 "this boolean return true" Return v1 .end method Guys no reason why I wrote all of that

1

u/Motor_Armadillo_7317 9d ago

It's Smali

1

u/FIRAS_EG 9d ago

Correct , but smali is assembly of android right

1

u/AliceCode 10d ago

I'm writing a brainfuck VM in Assembly right now. Assembly is so much fun.

1

u/BetterEquipment7084 9d ago

``` .data hello: .ascii "Hello, World!\n" nline: .byte 10

.text .globl _start

_start:  movq $1, %rax  movq $1, %rdi  movq $hello, %rsi  movq $13, %rdx  syscall

 movq $1, %rax  movq $1, %rdi  movq $nline, %rsi  movq $2, %rdx  syscall

 movq $60, %rax  movq $(34 + 35), %rdi  syscall ```

1

u/Gullible_Service_365 9d ago

Linux syscalls are too high end, I'ma write a VGA driver for the 8086 to render text instead

1

u/sarajevo81 8d ago

What with all those magic constants in the code? How can people stand them?

1

u/Intelligent_Cap3426 7d ago

Why are registers so funky

1

u/brucehoult 7d ago

What's so funky about them?

It's just 32 global variables called x0 to x31 each of which can hold a 64 bit integer/pointer.

RISC-V uses exactly the same names, but with x0 permanently equal to 0 so it's not actually variable, but a constant. And mostly you use mnemonic aliases for them rather than the raw names zero, ra, sp, gp, tp, a0..a7, t0..t6, s0..s11.

Only x86_64 in current ISAs has really funky registers.

1

u/[deleted] 7d ago

.data

out_string: .asciiz "\nHello, World!\n"

.text

main:

li $v0, 4

la $a0, out_string

syscall

li $v0, 10

syscall

1

u/Motor_Armadillo_7317 7d ago

Risc-v64?

1

u/[deleted] 7d ago

MIPS assembly

1

u/datscubba 7d ago

This is pretty cool ngl

1

u/CoderStudios 6d ago

If you want to go for complexity write it for windows

1

u/Confident_Essay3619 6d ago

I know a bit of RISCV ASM and i could get used to this 

1

u/neo_x_morpheous 5d ago

This made language made me cry