0

My question is, I don't understand print_string and read_int parameters. I read other questions and I understood how parameters are pushed on stack. I don't see that happening here. It makes sense that print_string has one parameter, which is in eax. I've read on wikipedia article x86 calling conventions that parameters can be passed in registers.

If someone could explain which parameters take both of then and why, I'd appreciate it. Thank you.

_asm_main:

enter   0,0 
pusha
mov eax, prompt
call    print_string
call    read_int
mov [input], eax

and

print_string:

enter   0,0
pusha
pushf
push    eax
push    dword string_format
call    _printf
pop ecx
pop ecx

popf
popa
leave
ret

and read_int:

enter   4,0
pusha
pushf
lea eax, [ebp-4]
push    eax
push    dword int_format
call    _scanf
pop ecx
pop ecx

popf
popa
mov eax, [ebp-4]
leave
ret
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    `print_string` takes one arg in `eax` and passes it to `printf(string_format, arg)` (which uses the standard stack-args calling convention). `print_string` saves/restores all the registers, and is very inefficient (or is optimized for code-size at the expense of speed?). `read_int` is basically the same, except it returns in `eax`. – Peter Cordes Nov 28 '17 at 13:32
  • IDK what kind of answer you're looking for. I mean obviously passing args in registers often takes fewer instructions, because you don't have to load them before using them. So passing args in registers was the obvious design choice for the x86-64 ABI (Windows and non-Windows), because it avoids the store/reload latency of pushing args and then reloading them. – Peter Cordes Nov 28 '17 at 13:34
  • Looks like Irvine lib implementation. Most (all?) of Irvine32 lib functions take arguments in registers (including `eax`), return values in `eax`, and preserve all registers. It's a bit inefficient, but makes it simpler to use by beginners, so they don't need to think about preservation of registers before calling the library function. You can of course read about expected arguments/results in the irvine lib docs, for particular library function. The called `_printf/_scanf` look to be ordinary C library runtime functions, expecting stack arguments (probably 32b `stdcall` calling conv.). – Ped7g Nov 28 '17 at 13:35
  • 1
    Related: [how the x86-64 System V ABI (calling convention) was designed, and why it's different from the x86-64 Windows calling convention](https://stackoverflow.com/a/35619528/224132). Also: https://stackoverflow.com/questions/38955064/why-parameters-stored-in-registers-and-not-on-the-stack-in-x86-64-assembly directly asks why register-args calling conventions were chosen. Basically x86-64 is a new architecture, so it's a chance to choose a new calling convention. Register-args are better in 32-bit mode, too, but the standards are old and changing a calling convention breaks compat. – Peter Cordes Nov 28 '17 at 13:39
  • thank you so much ! I still cannot up-vote or accept answer, but great answers – profi profil Nov 28 '17 at 13:59
  • These are just question comments, because neither of us bothered to produce full-fledged answer with all details included, also Peter is probably looking for decent duplicate to close this without answer at all. You may collect all that into your own self-answer if you find this information interesting and complete enough, then you can self-accept it after certain delay. This is generally welcomed behaviour (better than having unanswered/unclosed question), if the answer is of high quality (you may add any other research you did on your own filling up any gaps). The Irvine lib uses custom conv – Ped7g Nov 28 '17 at 14:36
  • @Ped7g: I'm still not sure exactly what the OP here really wants to know, so I'm not looking for a duplicate. @ profi: is this actually a duplicate of either or both of those? If not, please [edit] your question to be more clear about what kind of answer you're looking for. Or if my comments answer your question, let me know and I'll post them as an answer. – Peter Cordes Nov 28 '17 at 14:52

0 Answers0