Last active
August 29, 2015 14:03
-
-
Save topher6345/e72dd468c04d42d2a138 to your computer and use it in GitHub Desktop.
Various Algorithms to swap variables in Simplified assembly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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