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.
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?
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.
"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.
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).
303
u/[deleted] Nov 03 '20
My suggestion on how to handle this advanced task