0% found this document useful (0 votes)
6 views

Data Computer

Uploaded by

Alisha Waseem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Data Computer

Uploaded by

Alisha Waseem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

What is Swap in Assembly Language?

•Swapping is an act of exchanging one thing for another.


•A swap instruction, which exchanges a value in memory with a value of a register, is available on
many architectures.
•The primary application of a swap instruction has been for process synchronization.

How Can we Swap Two Numbers?


•Load first number in register AL through memory.
•Move the content of register AL in register BL.
•Load the second number in register AL through memory.
•Store the content of register AL at the memory location of first number.
•Store the content of register AL at the memory location of second number.
•Terminate the program

Loading…
Swapping Numbers
The act of swapping two variables refers to mutually exchanging the values of the variables.
Syntax :
MOV AL, [0600]
MOV BL, AL
MOV AL, [0601]
MOV [0600], AL
MOV [0601], BL
HLT
INPUT: 0600:05 0601:04
OUTPUT: 0600:04 0601:05
Swapping Number
•The following code in x86 assembly language uses the xor swap algorithm to swap the value in the
AX register with the value in the BX register without using a temporary buffer.
XOR AX, BX
XOR BX, AX
XOR AX, BX
•However, all x86 microprocessors have an XCHG instruction which does the same work on its
operands more efficiently than the above sequence of XORs.
Program to Swap Two numbers using Stack in assembly language
MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AX,'6'
MOV BX,'8'
PUSH AX
PUSH BX
POP AX
POP BX
MOV DX,AX
MOV AH,2
INT 21H
MOV DX,BX
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
Program to swap two number:

.MODEL SMALL
.STACK 100H
.DATA
NUM1 DB 05H
NUM2 DB 02H
.CODE
MOV AX , @DATA ; Initializing Data Segment
MOV DS , AXburning fuels, effective feedstocks for petrochemical operations, and more effective
lubricating oils. Only through hydro-cracking can heavy fuel oil components be
converted to transportation fuels and lubricating oils whose quality will meet tightening
environmental and market demands. Hydro-processing

; Storing Data
MOV BL , NUM1
MOV CL , NUM2

; Swapping
MOV NUM2 , BL
MOV NUM1 , CL

MOV AH , 4CH ; Interrupt to exit


INT 21H
END

You might also like