Back to the 2022 paper

Module 3: Instruction Set and Programming

20227m

What is Subroutine? Explain the execution of Subroutine function.

Worked SolutionAI Assisted

Subroutine & Its Execution

Definition: Subroutine (procedure/function) is reusable code block called from main via CALL, returns via RET/RETI.

Execution in 8051:

  1. LCALL/ACALL addr pushes PC (return address) onto stack (SP++, low then high), loads subroutine address into PC, jumps.
  2. Subroutine executes; may use PUSH/POP to preserve registers.
  3. RET pops PC (high/low) from stack, resumes after CALL. RETI also clears interrupt priority for ISRs.

Types: LCALL (16-bit), ACALL (11-bit page). Nesting limited by stack size. Benefits: modularity, code saving. Example:

LCALL DELAY
;...
DELAY: MOV R7,#255
LOOP: DJNZ R7,LOOP
       RET

Similar questions

Free AppAndroid App

Coursify Android App

Generate AI courses, take quick quizzes between classes, and master complex engineering concepts on the go.