Skip to content

Stack and data movements operations

PaulNowak36 edited this page Feb 22, 2024 · 3 revisions

mov instruction variants

The mov instruction is crucial for transferring data between registers, variables, and immediate values.

Register to immediate value

  • Syntax: mov rg2, 7
  • Binary Code: 0000 0010 0000 0111 (Op code: 0 0000)
  • Usage: Loads a number directly into a register.

Register to register

  • Syntax: mov rg2, rg7
  • Binary Code: 0001 1111 0000 1110, 0000 0010 0000 0000 (Op code: 0 0000)
  • Usage: Copies the value from one register to another.

Register to variable

  • Syntax: mov rg2, var2
  • Binary Code: 1011 1000 1101 0110 (var2 id), 0000 0010 0000 0000 (Op code: 0 0000)
  • Usage: Moves a variable's value into a register.

Variable to register

  • Syntax: mov var2, rg2
  • Binary Code: 0001 1010 0000 1110, 1100 0000 1101 0110 (var2 id) (Op code: 0 0000)
  • Usage: Stores a register's value into a variable.

Variable to variable

  • Syntax: mov var2, var1
  • Binary Code: 1011 1000 1101 0110 (var1 id), 1100 0000 1101 0110 (var2 id) (Op code: 0 0000)
  • Usage: Copies a value from one variable to another.

Variable to immediate value

  • Syntax: mov var2, 7
  • Binary Code: 1100 0000 1101 0110 (var2 id), 0000 0000 0000 0111 (Op code: 0 0000)
  • Usage: Assigns an immediate value to a variable.

mov instruction usage:

  • Loading an Immediate Value:
    mov rg1, 5   ; Load 5 into register 1
  • Copying between registers:
    mov rg2, rg1 ; Copy the value of rg1 to rg2
  • Variable manipulation:
    mov rg3, varName  ; Move the value of varName into rg3
    mov varName, rg3  ; Store the value of rg3 in varName

Stack operations in the Virtual Processor's assembly language are crucial for managing data in a Last-In-First-Out (LIFO) manner, particularly for subroutine calls and local variable storage. Additionally, the mov instruction is essential for various data transfer operations.

push and pop instructions

push

  • Syntax: push rgX
  • Function: Pushes the value in register rgX onto the stack.
  • Binary Code: Dependent on the specific register, e.g., 0010 0010 0000 0000 for push rg2.
  • Usage: Used for saving the current state of a register before modifying it or for preparing arguments for subroutine calls.

pop

  • Syntax: pop rgX
  • Function: Pops the top value from the stack into register rgX.
  • Binary Code: Based on the specific register, e.g., 0011 0010 0000 0000 for pop rg2.
  • Usage: Typically used to restore the value of a register or to retrieve subroutine call results.

pusha and popa instructions

pusha

  • Syntax: pusha
  • Function: Pushes the contents of all registers onto the stack.
  • Binary Code: 0001 1000 0000 1100 for the pusha instruction.
  • Usage: Used for saving the current state of all registers, typically at the beginning of a subroutine.

popa

  • Syntax: popa
  • Function: Pops values from the stack into all registers, except for register 3 which is read-only.
  • Binary Code: 0001 1000 0000 1101 for the popa instruction.
  • Usage: Used to restore the state of all registers, typically at the end of a subroutine.

Using pusha & popa:

  • Subroutine example:
    subroutine:
        pusha               ; Push all register values onto the stack
        ; Perform various operations
        popa                ; Pop all register values back from the stack
        ret                 ; Return from the subroutine
    main:
        call subroutine     ; Call the subroutine
        ; Execution continues here after the subroutine returns
    

LIFO method

Definition

LIFO (Last-In First-Out) is a method used for processing data where the most recently added item is the first to be removed. For example: if we put a book on a pile of book, if someone wants to read one, he will most likely pick up the one at the top, which was the last added.

Nowadays, the methid isn't very common due to its more complex and the involvement of more delicate stocks. Indeed, its use depends on the type of merchandise to be stocked, as some goods can deteriorate or become obsolete, rendering them unsellable if kept for too long.

Let's imagine a supermarket manager stocking merchandises: at first, alcohol is a type of merchandise that can benefit from longer storage since it requires aging in a cellar. Similarly, dry goods such as grains, sand, or gravel may be subject to inflation, making the LIFO method better suited for their management.

Usage

In this project, the LIFO is used to take the value of one register in the stack. Then, we could take it back at any moment when needed.

Example

; Example of LIFO use with the register 0

    mov rg0, 10
    push rg0 ; stack[0] = rg0 == 10                                                       
    pop ; stack[0] = NULL (empty)
    ; ...
Clone this wiki locally