assemblycpu-registersx86-1616-bit

swapping 2 registers in 8086 assembly language(16 bits)


Does someone know how to swap the values of 2 registers without using another variable, register, stack, or any other storage location? thanks!

Like swapping AX, BX.


Solution

  • You can do it using some mathematical operation. I can give you an idea. Hope it helps!

    I have followed this C code:

    int i=10; j=20
    i=i+j;
    j=i-j;
    i=i-j;
    

    mov ax,10
    mov bx,20
    add ax,bx  
    //mov command to copy data from accumulator to ax, I forgot the statement, now ax=30
    sub bx,ax //accumulator vil b 10
    //mov command to copy data from accumulator to bx, I forgot the statement now 
    sub ax,bx //accumulator vil b 20
    //mov command to copy data from accumulator to ax, I forgot the statement now