Abstract Data types or more colloquially data stuructures, are the complex items of information.

Subroutines

libraries the collections of some fragments available to user programmer
Such program fragments are called subroutines, or alternatively, procedures, or in C terminology, functions.

Call / Return Mechanism

image.png

We uses the call/return mechanism to direct the computer each time via the call instruction to the code A, and after the computer execute the code A, to the return instruction to the proper next instruction to be executed in the program.

caller the program that contains the call
callee the subroutine that contains the return

JSR / JSRR

for Jump to SubRoutine Opcode Chapter 8 Data Structures - 图2 Operand

  • Chapter 8 Data Structures - 图3 Specification digit of addressing mode
    • JSR PC-relative if its value is 1,
      the second operand is obtained by sign-extending bitsChapter 8 Data Structures - 图4to 16 bits.
    • JSRR Base Register if its value is0,
      the second operands is the value in SR2 specified by bitsChapter 8 Data Structures - 图5.
      Chapter 8 Data Structures - 图6must be 0.

FUNTION Chapter 8 Data Structures - 图7 Condition Codes ✗ image.png

RET

Opcode Chapter 8 Data Structures - 图9 Operand

  • Chapter 8 Data Structures - 图10 must be 000 111 000000

FUNTION Chapter 8 Data Structures - 图11 Condition Codes ✗ image.png

Saving and Restoring Registers

If a value in a register will be needed after someting else is stored in that register, we must save it before something else happens and restore it before it can be subsequently used.
A register value is saved by storing it in memory, and is restored by loading it back into the register.
callee save the save/restore problem is handled by the callee.
caller save the save/restore problem is handled by the caller. e.g. R7

The Stack

Property / Stack Protocol

The last thing stored in the stack is the first thing to remove.
Simply put : Last In, First Out, or LIFO.

Implementaion

stack pointer, sp

It keeps track of the top of the stack. In LC-3, R6 is the stack pointer. Note the values inserted into the stack are stored in memory locations having decreasing addresses. We say the stack grows toward zero.

Push

push a value onto the stack. overflow we can’t push values where there is no space. R5 is used to indicatie success ( R5 = 0 ) or failure ( R5 = 1 ) information.

  1. ; push the value in R0 onto the stack.
  2. PUSH ST R1, PUSH_SAVE1
  3. LD R1, STACK_MAX
  4. NOT R1, R1
  5. ADD R1, R1, #1
  6. ADD R1, R1, R6
  7. BRz OVERFLOW
  8. ADD R6, R6, #-1
  9. STR R0, R6, #0
  10. AND R5, R5, #0
  11. LD R1, PUSH_SAVE1
  12. RET
  13. OVERFLOW AND R5, R5, #0
  14. ADD R5, R5, #1
  15. LD R1, PUSH_SAVE1
  16. RET
  17. STACK_MAX .FILL ADDR ; the topmost address of stack
  18. PUSH_SAVE1 .BLKW #1

Pop

pop a value from the stack. underflow we can’t pop values when the stack is empty. R5 is used to indicatie success ( R5 = 0 ) or failure ( R5 = 1 ) information.

  1. ; Lazy Pop, without delete the element
  2. ; pop the value from the stack into R0.
  3. POP LD R0, STACK_BASE
  4. NOT R0, R0
  5. ADD R0, R0, #1
  6. ADD R0, R0, R6
  7. BRz UNDERFLOW
  8. LDR R0, R6, #0
  9. ADD R6, R6, #1
  10. AND R5, R5, #0
  11. RET
  12. UNDERFLOW AND R5, R5, #0
  13. ADD R5, R5, #1
  14. RET
  15. STACK_BASE .FILL ADDR ; the bottommost address of the stack

Recursion

Recursion is a mechanism for expressing a function in terms of itself.
Stack is used to implement the recursion. It stores the return linkage R7, the values used to solve the problem, and the other values concerning the save/restore problem.

Example : Factorial

  1. ; Suppose we have MUL instruction.
  2. FACT ADD R6, R6, #-1
  3. STR R1, R6, #0 ; Push caller's R1
  4. ADD R1, R0, #-1
  5. BRz BASE_CASE ; n = 1 is the base case.
  6. ADD R6, R6, #-1
  7. STR R7, R6, #0 ; Push return linkage
  8. ADD R6, R6, #-1
  9. STR R0, R6, R0 ; Push n
  10. ADD R0, R0, #-1
  11. JSR FACT
  12. LDR R1, R6, #0
  13. ADD R6, R6, #1 : Pop n
  14. MUL R0, R0, R1 ; R0 <- n * (n - 1)!
  15. LDR R7, R6, #0
  16. ADD R6, R6, #1 ; Pop return linkage
  17. BASE_CASE LDR R1, R6, #0
  18. ADD R6, R6, #1 ; Pop caller's R1
  19. RET

image.png

The Queue

Property

First in First Out, FIFO.

Implementation

FRONT pointer & REAR pointer

In LC-3, R3 is the FRONT pointer and R4 is the REAR pointer.

Remove from Front & Insert at Rear

Considering wrap around, underflow and overflow. R5 is used to indicatie success ( R5 = 0 ) or failure ( R5 = 1 ) information.

  1. ; don't consider queue initiation!
  2. INSERT ST R1, QUQUE_SAVE1
  3. AND R5, R5, #0
  4. LD R1, NEG_LAST
  5. ADD R1, R1, R4
  6. BRnp INSERT_SKIP_1
  7. LD R4, FIRST ; Wrap around, R4 <- FIRST
  8. BRnzp INSERT_SKIP_2
  9. INSERT_SKIP_1 ADD R4, R4, #1 ; No wrap around, R4 <- R4 + 1
  10. INSERT_SKIP_2 NOT R1, R4
  11. ADD R1, R1, #1 ; R1 = - REAR
  12. ADD R1, R1, R3 ; R1 <- FRONT - REAR
  13. BRz OVERFLOW
  14. STR R0, R4, #0 ; Do the insert
  15. BRnzp INSERT_DONE
  16. OVERFLOW LD R1, NEG_FIRST
  17. ADD R1, R1, R4
  18. BRnp INSERT_SKIP_3
  19. LD R4, LAST ; Wrap around, R4 <- LAST
  20. BRnzp INSERT_SKIP_4
  21. INSERT_SKIP_3 ADD R4, R4, #-1 ; No wrap around, R4 <- R4 - 1
  22. INSERT_SKIP_4 ADD R5, R5, #1
  23. INSERT_DONE LD R1, QUEUE_SAVE1
  24. RET
  25. REMOVE ST R1, QUQUE_SAVE1
  26. AND R5, R5, #0
  27. NOT R1, R4
  28. ADD R1, R1, #1 ; R1 = - REAR
  29. ADD R1, R1, R3 ; R1 = FRONT - REAR
  30. BRz UNDERFLOW
  31. LD R1, NEG_LAST
  32. ADD R1, R1, R3
  33. BRnp REMOVE_SKIP_1
  34. LD R3, FIRST ; Wrap around, R3 <- FIRST
  35. BRnzp REMOVE_SKIP_2
  36. REMOVE_SKIP_1 ADD R3, R3, #1 ; No wrap around, R3 <- R3 + 1
  37. REMOVE_SKIP_2 LDR R0, R3, #0 ; Do the remove
  38. BRnzp REMOVE_DONE
  39. UNDERFLOW ADD R5, R5, #1
  40. REMOVE_DONE LD R1, QUEUE_SAVE1
  41. RET
  42. LAST .FILL ADDR1 ; the topmost memory space
  43. NEG_LAST .FILL -ADDR1
  44. FIRST .FILL ADDR2 ; the bottommost memory space
  45. NEG_FIRST .FILL -ADDR2
  46. QUEUE_SAVE1 .BLKW 1

:::warning

How many elements can be stored in a Queue?

In order to differentiate the full queue and the empty queue, we only allow a queue to store onlyChapter 8 Data Structures - 图14elements ifChapter 8 Data Structures - 图15elements has been allocated.<br />A queue is full when there are![](https://cdn.nlark.com/yuque/__latex/34a8d91d127e690860396e36a4736d1e.svg#card=math&code=n%20-%201%20&id=GFx9J)elements in the queue, i.e.FRONT - 1 == REAR.<br />A queue if full whenFRONT == REAR`.
image.png :::

Character Strings

A one-dimensional array of ASCII codes, and at the end the null characterChapter 8 Data Structures - 图17indicates the end of the character string.