0% found this document useful (0 votes)
8 views10 pages

Assembly Language ch8

The document discusses arrays and stacks in assembly language. It provides examples of defining and accessing arrays, as well as using push and pop instructions to manipulate a stack. Arrays allow storing multiple values in contiguous memory locations, accessed via indexes. Stacks follow LIFO order, using push to add and pop to remove values, managed via the stack pointer.

Uploaded by

mahdiware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Assembly Language ch8

The document discusses arrays and stacks in assembly language. It provides examples of defining and accessing arrays, as well as using push and pop instructions to manipulate a stack. Arrays allow storing multiple values in contiguous memory locations, accessed via indexes. Stacks follow LIFO order, using push to add and pop to remove values, managed via the stack pointer.

Uploaded by

mahdiware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Microprocessor

and
Assembly Language

RED SEA UNIVERSITY


FACULTY OF COMUPTER SCIENCE & IT
Lecturer: Eng. Muhiyadin Abdikadir
Chapter Eight
Array and Stack
Arrays
Arrays in assembly language are essentially sequences of data elements stored in
contiguous memory locations.
To define and manipulate arrays, you typically use directives to reserve space and registers
to access the elements. Here's a basic example to illustrate how you might work with arrays
in emu8086:

myArray: This is a label that you've given to the location in memory where the array starts. It's
like naming a variable in higher-level programming languages.
DB: This stands for "Define Byte". It's an assembler directive used to allocate memory for
variables. Each DB allocates one byte of memory. 02
'A', 'B', 'C', 'D', 'E': These are the values that you're storing in the array. Each one is a
character literal, and since it's enclosed in single quotes, the assembler will store the ASCII
value of each character in consecutive memory locations.
So, myArray is an array with 5 elements, where each element is a single byte representing
the ASCII values of the characters 'A' to 'E'.

In memory, it would look something like this (with the ASCII values):

myArray: 41h, 42h, 43h, 44h, 45h


03
Example
An assembly language code that display the characters from ‘A’ to ‘E’ using an Array.

• mov si, 0: This sets the SI register to 0. SI is used as an index to iterate over the array.
• The array_loop label marks the beginning of a loop that will execute until SI is equal to 5.
04
• MOV dl, [myArray + si]: This moves the byte at the
current index of myArray into the DL register. DL is
used because the DOS interrupt for displaying
characters expects the character to be in DL.

• inc si: This increments the SI register to move to the next element in the array.
• cmp si, 5: This compares SI to 5 to check if the end of the array has been reached.
• jl array_loop: If SI is less than 5, the jump if less instruction causes the program to loop
back to sum_loop and continue with the next element. The code will display characters 'A'
to 'E' one by one on the screen.

05
Stacks
A stack is a special area of computer's memory which stores temporary variables. The stack is a
"last-in, first-out" (LIFO) data structure, meaning that the last variable pushed onto the stack is the
first one to be popped off. Here's how the stack works in emu8086:

• PUSH: This instruction places a value onto the top of the stack. The
stack pointer (SP) is decremented to make room for the new item.

Push 1 Pop ax
• POP: This instruction removes the value from the top of the stack
Push 2 Pop bx
and places it into a specified register. The stack pointer (SP) is Push 3 Pop cx
incremented as the stack shrinks. Push 4 Pop dx
Push 5 Pop es

The stack pointer (SP) is a register that points to the current top of the stack. In emu8086, the
stack grows downwards, so PUSH decrements SP and POP increments SP.
06
PUSH and POP instruction are especially useful because we don't have too much registers to operate
with, so here is a trick:
Syntax for PUSH instruction:
• Store original value of the register in stack (using PUSH). PUSH REG
• Use the register for any purpose. PUSH SREG
PUSH memory
• Restore the original value of the register from stack (using POP).
PUSH immediate
Here is an example: REG: AX, BX, CX, DX, DI, SI, BP, SP.
SREG: DS, ES, SS, CS.
memory: [BX], [BX+SI+7], 16 bit variable, etc...
immediate: 5, -24, 3Fh, 10001101b, etc...

Syntax for POP instruction:


POP REG
Another use of the stack is for exchanging the values, POP SREG
POP memory
REG: AX, BX, CX, DX, DI, SI, BP, SP.
SREG: DS, ES, SS, (except CS).
memory: [BX], [BX+SI+7], 16 bit variable, etc...

07
Another Example ; Initialize the stack segment
• mov ax, 10h ; Move hexadecimal value 10h into AX register
• mov bx, 20h ; Move hexadecimal value 20h into BX register
• mov cx, 30h ; Move hexadecimal value 30h into CX register
• mov ss, ax ; Move the value of AX into SS (Stack Segment register)
• mov sp, 0FFFFh ; Set Stack Pointer (SP) to the top of the stack segment

This section sets up the stack segment by moving the hexadecimal value 10h into the SS
register, which defines the starting segment of the stack. The stack pointer SP is set to
0FFFFh, pointing to the top of the stack.
; Push values onto the stack
push ax ; Push the value of AX register onto the stack
push bx ; Push the value of BX register onto the stack
push cx ; Push the value of CX register onto the stack

Here, the values in the AX, BX, and CX registers are pushed onto the stack in that order. The SP is
automatically decremented by 2 for each push since the stack in the x86 architecture is 16 bits wide.
08
mov ax, 1 ; Move the value 1 into AX register
mov bx, 2 ; Move the value 2 into BX register
mov cx, 3 ; Move the value 3 into CX register

The AX, BX, and CX registers are then overwritten with new values 1, 2, and 3, respectively. This
demonstrates that the original values have been safely stored on the stack.

; Pop values from the stack


pop cx ; Pop the top value of the stack into CX register
pop bx ; Pop the next value of the stack into BX register
pop ax ; Pop the next value of the stack into AX register

Finally, the values are popped off the stack in reverse order. The last value pushed (CX with 30h) is
the first to be popped off into CX, followed by BX (20h), and AX (10h). The SP is automatically
incremented by 2 for each pop.
After executing this code, the CX, BX, and AX registers will contain the values 30h, 20h, and 10h,
respectively, which were the original values before they were changed to 3, 2, and 1. This
demonstrates the LIFO (Last-In-First-Out) nature of the stack. The stack is used in this
09
example to temporarily save and restore register values.
Quiz
1) Write an assembly language program that exchanges three values using stack.

2) Write an assembly language Program that display digits from 1 to 6 using an Array.

End
10

You might also like