0

I am practicing interpreting assembly from my textbook and I'm currently stuck on this problem. Suppose I have the following function sun given in x86-64 assembly:

00000000004005f7 <sun>:
  4005f7: 89 f8             mov %edi,%eax
  4005f9: 83 ff 01          cmp $0x1,%edi
  4005fc: 7f 02             jg 400600 <sun+0x9>
  4005fe: f3 c3             repz retq
  400600: 53                push %rbx
  400601: 89 f3             mov %esi,%ebx
  400603: c1 fe 02          sar $0x2,%esi
  400606: 8d 7f ff          lea -0x1(%rdi),%edi
  400609: e8 e9 ff ff ff    callq 4005f7 <sun>
  40060e: 01 d8             add %ebx,%eax
  400610: 5b                pop %rbx
  400611: c3                retq

What I am supposed to do is to list the callee-saved registers (if any) that are used and to list the caller-saved registers (if any) that are used. However, what I am having trouble with is how to distinguish between callee-saved and caller-saved registers within the assembly. I am aware of x86-64 register saving conventions, but I'm unsure if that applies to this assembly. For example, for callee-saved registers, I believe it would just be %rbx. For caller-saved registers, I think it would just be %rdi. Are there more callee-saved or caller-saved registers I am not seeing? Any feedback or suggestion would be greatly appreciated in helping me figure out how to distinguish between callee-saved and caller-saved registers.

  • You have to choose the applicable ABI, then use their docs or an overview something like: https://www.dyncall.org/docs/manual/manualse11.html – Erik Eidt May 06 '21 at 19:57
  • Yup, just the one push/pop, no other call-preserved registers used. For call-clobbered, you're missing RAX and RSI. This is obviously using the x86-64 System V calling convention / ABI, given that it takes its first arg in EDI. – Peter Cordes May 06 '21 at 20:05
  • @PeterCordes Are you sure this is a duplicate? I don't know if the OP is asking about the x64 ABI or if they are asking about *how* to recognize the ABI (if that is a meaningful difference anyway). – Margaret Bloom May 06 '21 at 20:06
  • @MargaretBloom. If they'd care to clarify on that point, we can reopen if necessary. Now that you mention it, I might have been hasty in interpreting *I am aware of x86-64 register saving conventions, but I'm unsure if that applies to this assembly.* I initialled missed the "*this* assembly" part. But still, given that everything it does makes sense for x86-64 SysV, closing as a duplicate is one way of saying "yes, it applies to this assembly" (which looks compiler generated, given the `repz ret` that older GCC used in -mtune=generic). – Peter Cordes May 06 '21 at 20:12
  • But yeah, if you entertain the possibility of hand-written asm using a custom calling convention, then you'd have to look at what it preserves: RBX is the only reg is writes that it preserves around the usage. – Peter Cordes May 06 '21 at 20:12
  • @MargaretBloom I am unaware of the term ABI since I never heard it being used in the class I am in. All I was asking was in my question is what the caller-saved and callee-saved registers are from the given assembly. I am not sure what else I can clarify, so please let me know. – John Issacs May 06 '21 at 20:29
  • @JohnIssacs What operating system is your assembly code supposed to run on? If it's Linux, then you're using the System V ABI. – Joseph Sible-Reinstate Monica May 06 '21 at 21:44
  • @JosephSible-ReinstateMonica I'm assuming it would be Linux because that's what my class is using. – John Issacs May 06 '21 at 23:19

0 Answers0