Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 3
    \$\begingroup\$ This isn't a function, just a snippet. You need a ret instruction as well (one byte). And that's assuming a custom calling convention where eax is used as an arg-passing register. (The Irvine32 asm teaching library does that, but no standard calling conventions for x86-32 or x86-64 do that, unlike on ARM where the return-value register r0 is also the first arg-passing register.) Custom calling conventions are fine in asm functions though, so you don't need to change it to 3 byte lea (%ecx, %edx), %eax + 1 byte ret for Microsoft __fastcall or gcc -mregparm. \$\endgroup\$ Commented Dec 17, 2017 at 19:20
  • 1
    \$\begingroup\$ It's an interesting challenge for a stack-args calling convention, though. pop is only 1 byte, but esp-relative addressing modes need a SIB byte. So a caller-pops calling convention might pop %ecx (the return address), pop %eax / pop %edx / add %edx, %eax (2 bytes) / jmp *%ecx (2 bytes) = 7 bytes total. vs. mov 4(%esp), %eax (4B) / add 8(%esp), %eax (4B) / ret = 9 bytes, or 11 bytes for a caller-pops version of that using ret imm16, if I did that right. It's an extra 1 byte for the SIB with ESP as a base, and an extra 1 byte for the disp8 in each insn. \$\endgroup\$ Commented Dec 17, 2017 at 19:26
  • \$\begingroup\$ @PeterCordes late reply since I haven't been on this site in years, but per the rules which you have even commented on, assembly submissions may take input and output from registers. \$\endgroup\$ Commented Dec 18, 2021 at 2:26
  • \$\begingroup\$ The meta Q&A you linked says an answer might consist of a subroutine. An x86 subroutine ends with a ret (or a jmp or something). Just like a C answer must be foo(x){return x+x;} not x+x. Yes, such functions are fully allowed to take args in registers, as per Tips for golfing in x86/x64 machine code, like a function with a custom calling convention. \$\endgroup\$ Commented Dec 18, 2021 at 2:32
  • \$\begingroup\$ Forgive me- I'm sure you're more educated on this topic than me, but while ending a subroutine in assembly with a ret or jmp may be conventional, there's nothing that really requires that. I think it's perfectly reasonable to put a label at the top of some n lines and call it a subroutine. Further, I did include a link to a full running program. \$\endgroup\$ Commented Dec 18, 2021 at 2:45