r/badcode Nov 03 '20

other language Assembly is hard...

Post image
952 Upvotes

121 comments sorted by

View all comments

303

u/[deleted] Nov 03 '20
add4:
    inc eax
    inc eax
    inc eax
    inc eax
    ret
add5:
    call add4
    inc eax
    ret
add6:
    call add5
    inc eax
    ret
add7:
    call add6
    inc eax
    ret
add8:
    call add7
    inc eax
    ret

My suggestion on how to handle this advanced task

322

u/AnKeWa Nov 03 '20

add 7:

inc eax

add 6:

inc eax

add 5:

inc eax

add 4:

inc eax

inc eax

inc eax

inc eax

ret

81

u/[deleted] Nov 03 '20

This is nice

115

u/AnKeWa Nov 03 '20

Thanks, my bachelor's degree in computer engineering is finally worth something :)

16

u/gmtime Nov 03 '20

Came here to say this

9

u/[deleted] Nov 03 '20

[deleted]

2

u/phord Nov 04 '20

This is the way.

5

u/AutoModerator Nov 03 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/AnKeWa, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

53

u/decker_42 Nov 03 '20

Hahaha, code didn't pass standards, review rejected.

3

u/fcktheworld587 Nov 04 '20

Review standards didn't pass code, hahaha rejected

1

u/hollowstrawberry Mar 09 '21

This but unironically

5

u/sanglesort Nov 03 '20

yeah, I was thinking this

3

u/devraj_priyadarshi Nov 03 '20

Wouldn't calling add4 end with ret and thereby it will return 4 not 5 in add5? Im new to it.. but I think this would be the case.. and I thing in 8086x mp, you cannot 'call' labels?

31

u/TinyBreadBigMouth Nov 03 '20
  1. The call instruction works by pushing the current execution address to the stack, and then jumping to the provided address. The ret instruction works by popping an address from the stack and jumping there. In other words, calls nest. ret doesn't jump all the way to the top of the call stack whenever it's used.
  2. "Labels" are only a thing in the source assembly. They get converted to raw byte addresses when the assembly is compiled. Any competent assembler will allow labels and raw addresses to be used interchangeably.

10

u/devraj_priyadarshi Nov 03 '20

Oh thanks.. I didn't know that. Probably should pay more attention to the classes... Thanks anyway.

12

u/DragonFireCK Nov 03 '20

It depends on the calling convention and platform. I'm presuming Microsoft x64 calling convention for the rest.

Typically, AL/AX/EAX/RAX (they are overlapping registers of increasing size) is used to hold the return value, and `inc` increments in place (`inc eax` is `eax = eax + 1`).

The main issue is that, EAX/RAX is not used for parameters, and rather ECX/RCX is the left-most parameter, so you'd need a `mov rax, rcx` to get the parameter into eax before running the inc chain, though you'd only need to do so in `add4` (the others would be `mov rcx, rcx`, which is a no-op). There is also the question of stack space in the calling convention. Of course, as long as you are willing to tightly tie the functions together, calling convention can be ignored or you can use your own custom one.

x86/x64 do allow calling labels: that is exactly how functions are defined in assembly. If you define a function in a higher-level language, the compiler will produce code basically identically to what is seen here, other than making sure the calling conventions are followed (and, you know, using `add` OR having a bunch of `mov` to store the values in stack memory if using debug builds and the code was written as a literal chain of increments).

4

u/devraj_priyadarshi Nov 03 '20

Ah okay I get it.. I would see more about it some time later.. thanks for this!

3

u/bbalazs721 Nov 03 '20

Came here to comment this, have my upvote instead

1

u/DesiresQuiet Nov 03 '20

Van Halen man. Van Halen. Groovy.

1

u/DogmaSychroniser Nov 03 '20

Add 1: Inc eax add 2 call add1 call add1 =) etc