correctly restore the saved registers of callees

Both ways are fine, but the second is a little more conventional.

The rule is this: when a compiler generates code to call a function, it assumes that the contents of ebx will be the same after the call as before. In your program, the only situation where that applies is to the startup code which calls main. For both versions of your code, ebx has the same value when main returns as when it was entered, so all is well.

If you had a C function in your program which called print, then the first version would be bad, and the second version would be needed. But as it stands in the first version, print is only called from your hand-coded main function, and you know that print will clobber ebx and you are taking appropriate action to save and restore it, so that’s fine.

To put it another way, in the first version of the code, print doesn’t conform to standard C calling conventions. But since you never actually call it from C code, that isn’t necessarily a problem. In the second version, it does conform, which is perhaps aesthetically nicer and may be less confusing to maintain.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top