AJ

CSCI2021 Lecture 18 Summary

Announcements
  • Project 3 posted

C Loop to Assembly
  • General structure:

    • Loop: while(<condition>) { <loop body> }

    • Assembly conversion:

    • .LOOP: cmpX

    • jXX .END

    • <asm loop body>

    • jmp .LOOP

    • .END:

    • <asm following code>

C If to Assembly
  • General structure:

    • If statement: if(<condition>) { <if body> }

    • Assembly conversion:

    • cmpX

    • jXX .AFTER

    • <asm if body>

    • .AFTER:

    • <asm following code>

Assembly Control Flow
  • Generalizations not always accurately translate to assembly.

  • Compilers may perform optimizations causing unexpected behavior.

Assembly: Procedures
  • Procedures are defined as blocks of code with arguments and return values.

  • Caller: procedure making the call.

  • Callee: procedure being called.

Procedure Call Mechanics:
  • Control flow managed via the instruction pointer %rip.

  • Passing data involves arguments through registers and returning values.

  • Local variables reside in the stack frame, which is allocated during the call.

Assembly Call and Return Operations
  1. Call: - Push return address onto stack.

    • Jump to the callee address.

  2. Return: - Restore return address from the stack.

Assembly Procedures: Passing Data
  • Data passed via registers.

  • If > 6 args, push onto caller's stack frame for callee retrieval.

  • Return values in %rax.

Local Variables
  • Stored in registers, handled via callee-saved and caller-saved conventions.

  • Callee saves register values onto the stack before use if the registers are in use.

C vs Assembly Procedure Calls
  • Comparisons made using control flow in recursive examples.

  • Real scenarios require thorough understanding of stack and frame allocations.