Laboratory Manual: For Computer Organization and Assembly Language
Laboratory Manual: For Computer Organization and Assembly Language
Laboratory Manual
for
Stack:
A stack is one dimensional data structure. Items are added
and remove from one end of structure i.e. it is processed in a “last-
in, first-out” manner. The most recent addition to the stack is called
top of the stack. A program must set aside a block of memory to
hold the stack. The stack is a special segment in memory used to
facilitate subroutine handling. We have been doing this by declaring
a stack segment; for example.
.STACK 100H
When the program is assembled and loaded in memory, SS will
contain the segment number of the stack segment and SP (stack
pointer) will initialize to 100h. This represents the empty stack
position as shown. When the stack is not empty, SP contains the
offset address of the top of the stack.
The PUSH instruction is used to store the content of a 16-bit register, or memory location, on the
stack. The stack handling instructions are summarized in Table6.1.
Table 6.1
The calling code calls a procedure with the CALL instruction, the procedure returns to the caller
with the RET instruction. A simple procedure may consist of nothing more than a sequence of instructions
ending with a ret instruction.
For example the following “procedure” zeros out the 256 bytes starting at the address in the BX register:
ZeroBytes PROC
XOR AX, AX
MOV CX, 128
ZeroLoop:
MOV [BX], AX
ADD BX, 2
LOOP ZeroLoop
RET
ZeroBytes ENDP
The CALL & RET Instruction:
The CALL instruction transfers the flow of the program to the procedure. The CALL instruction
differs from the jump instruction in the sense that a CALL saves a return address on the stack. The RET
instruction return control to the instruction that immediately follows the CALL as shown below.
Passing parameter:
Two common methods for parameter passing
Register Method
Stack Method
C Z S O P
0 R r 0 r
Invert each bit of the operand. Algorithm:
if bit is 1 turn it to 0.
if bit is 0 turn it to 1.
Example:
REG MOV AL, 00011011b
NOT Memory NOT AL ; AL = 11100100b
C Z S O P
Unchanged
Logical OR between all bits of two operands.
Result is stored in first operand. These rules
apply:
1 OR 1 = 1
1 OR 0 = 1
REG, memory 0 OR 1 = 1
memory, REG
0 OR 0 = 0
REG, REG
OR Example:
memory, immediate MOV AL, 'A' ; AL = 01000001b
REG, immediate OR AL,00100000b
;AL=01100001b('a')
C Z S O P A
0 R r 0 r ?
REG, memory Logical XOR (Exclusive OR) between all bits
memory, REG of two operands. Result is stored in first
XOR REG, REG operand. These rules apply:
memory, immediate 1 XOR 1 = 0
REG, immediate 1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0
Example:
MOV AL, 00000111b
XOR AL, 00000010b
;AL = 00000101b
C Z S O P A
0 R r 0 r ?
Logical TEST between all bits of two
operands for flags only. These flags are
effected: ZF, SF, PF. Result is not stored
anywhere. These rules apply:
1 TEST 1 = 1
REG, memory 1 TEST 0 = 0
memory, REG 0 TEST 1 = 0
REG, REG 0 TEST 0 = 0
TEST memory, immediate Example:
REG, immediate MOV AL, 00000101b
TEST AL, 1 ;ZF = 0.
TEST AL, 10b ;ZF = 1.
C Z S O P
0 r r 0 r
Table 6.2
Note:
These marks are used to show the state of the flags:
1 - instruction sets this flag to 1.
0 - instruction sets this flag to 0.
r - flag value depends on result of the instruction.
? - flag value is undefined (maybe 1 or 0).