Skip to content

Instantly share code, notes, and snippets.

@topher6345
Last active August 29, 2015 14:03
Show Gist options
  • Save topher6345/e72dd468c04d42d2a138 to your computer and use it in GitHub Desktop.
Save topher6345/e72dd468c04d42d2a138 to your computer and use it in GitHub Desktop.
Various Algorithms to swap variables in Simplified assembly
; Swap Registers A & B with no tmp
JMP start
ARG1: DB 13 ; Variable
ARG2: DB 3 ; Variable
start:
MOV A, [ARG1] ; A = ARG1;
MOV B, [ARG2] ; B = ARG2;
call swap
DB 0 ; exit();
swap: ; swap(A,B)
SUB A, B ; a = a - b
ADD B, A ; b = a + b
SUB B, A ; b = b - a
RET
; Swap Registers A & B with tmp
JMP start
ARG1: DB 13 ; Variable
ARG2: DB 3 ; Variable
start:
MOV A, [ARG1] ; A = ARG1;
MOV B, [ARG2] ; B = ARG2;
call swap
DB 0 ; exit();
swap: ; swap(A,B)
MOV C, A
MOV A, B
MOV B, C
RET
; Swap Registers A & B with XOR
JMP start
ARG1: DB 13 ; Variable
ARG2: DB 3 ; Variable
start:
MOV A, [ARG1] ; A = ARG1;
MOV B, [ARG2] ; B = ARG2;
call swap
DB 0 ; exit();
swap: ; swap(A,B)
XOR A, B ; a = a ^ b
XOR A, B ; a = a ^ b
XOR B, A ; b = b ^ a
XOR B, A ; b = b ^ b
RET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment