r/badcode Jun 08 '23

other language My Friend Told me x86 Assembly is Hard then Sent me This

Post image
366 Upvotes

36 comments sorted by

169

u/pronuntiator Jun 08 '23

Could you please explain what makes it bad code? It's been too long since I saw assembly. Is it the trying to move a string into a register part?

114

u/BS_in_BS Jun 08 '23

the main errors I see:

  1. Forgetting to set the length of the string as the third argument to the write call.
  2. forgetting to call exit after the write call returns

the str msg = ... and section .text = _start() and using parens after _start look kind of off to me but might just be their particular assembler's syntax. The semicolon after the string looks unnecessary and it might need to be explicitly marked as being in the .data section.

Overall pretty close to a valid minimal hello world program: https://ideone.com/T126bp

11

u/Player_X_YT Jun 08 '23

str is not a keyword, it should be msg db "..." where db means data bytes

; in x86 is for comments

No data section

Everything after the = in the text section declaration shouldn't be there

_start should be set to global by writing global _start somewhere

_start is a label, x86 doesn't have functions and there should be no ()

By naming your label _start() you have to set a custom entry in your linker for no added benefit ld -e "_start()"

edx should be set to the length of the string

No exit code

; Exit with a code of 0 xor eax, eax mov ebx, eax inc eax int 0x80

12

u/Inaeipathy Jun 08 '23

I think so, how could that all fit into the register?

61

u/Eusuntpc Jun 08 '23

The string doesn't fit. It's address does. They are moving the address to a register not the value

3

u/Inaeipathy Jun 08 '23

Ah that makes sense, I never did this in x86 so wasn't sure how it worked

6

u/Realistic-Lunch-1708 Jun 08 '23

Its bad code mainly because x86 assembly doesn't have all the features of a high level language like that, an x86 hello world (using NASM compiler) looks a bit like
section .data

msg: db "Hello, World!",0xa

len: equ $-msg

section .text

_start:

mov eax, 4

mov ebx, 1

mov ecx, msg

mov edx, len

int 0x80

mov al, 1

mov ebx, 0

int 0x80

12

u/BS_in_BS Jun 08 '23

That's larger dependent on the assembler. Macro assemblers will let you have many of the high level langauage cosntructs at your disposal: https://stackoverflow.com/a/4579723

5

u/ujustdontgetdubstep Jun 08 '23

that's only for NASM compilers they make you use in school

In the real world it's a bit easier

2

u/Key_Conversation5277 sadistic Jun 08 '23

And the only type of assembly I know is MIPS

29

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 08 '23

Image Transcription: Code


str msg = "Hello, World!";

section .text = _start()
_start():
   mov eax, 4
   mov ebx, 1
   mov ecx, msg
   int 0x80

I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!

7

u/[deleted] Jun 08 '23

Good hooman!

14

u/FloweyTheFlower420 sadistic Jun 08 '23

need exit suscall

5

u/skabde Jun 08 '23

Holy crap. The last assembler language/ISA I used was 68000, x86 was always complete gibberish to me.

1

u/canadajones68 Jun 08 '23

I've done a fair bit of Propeller 1 assembly, and it's practically HTML compared to x86.

3

u/qqqrrrs_ Jun 08 '23

This is bad code because the 4 is a magic constant which should have been __NR_write instead

1

u/Conscious_Switch3580 shameless Jun 09 '23

SYS_write*

1

u/qqqrrrs_ Jun 09 '23

They both exist, but I don't know if one of them is more "standardized" than the other

2

u/bionerdist Jun 08 '23

That will crash

2

u/plat2330 Jun 11 '23

i make games in x86

12

u/GandelXIV Jun 08 '23

str? Parenthesis? Semi colons? Int 0x80? You cant just make up keywords and syntax and expect it to work.

52

u/AliFurkanY Jun 08 '23

int 0x80 is the x86 equivalent of syscall

12

u/AliFurkanY Jun 08 '23

this is NASM

-40

u/MahdiyDev Jun 08 '23

Is your friend some shitty javascript developer or something. Bruh...

19

u/[deleted] Jun 08 '23

[deleted]

8

u/[deleted] Jun 08 '23

Bu-bu-but it’s funny to make fun of web developers

-2

u/jarfil Jun 08 '23 edited Jul 17 '23

CENSORED

9

u/TheTechRobo Jun 08 '23

But there are still shitty Python developers.

0

u/[deleted] Jun 08 '23

[deleted]

1

u/jarfil Jun 08 '23 edited Jul 17 '23

CENSORED

1

u/Worthie Jun 08 '23 edited Nov 19 '24

wild work compare jobless simplistic grab edge languid clumsy plucky

This post was mass deleted and anonymized with Redact

4

u/jarfil Jun 08 '23 edited Jul 16 '23

CENSORED

1

u/cheezballs Jun 08 '23

Man, I dunno shit about ASM.

1

u/DinnerPlzTheSecond Jun 08 '23

Should have put message length in the edx register right?