Created
April 14, 2016 23:41
-
-
Save Riketta/414fcd7420ad93ca2c7bf58b7a871ea5 to your computer and use it in GitHub Desktop.
hex2bin throw inline assembly
This file contains 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
#include <iostream> | |
#include <stdio.h> | |
#include <stdlib.h> | |
using namespace std; | |
int main() | |
{ | |
cout << "Enter hex nubmer:" << endl; | |
int n = 0; | |
cin >> hex >> n; | |
cout << "Entered: 0x" << hex << n << endl; | |
//for (int i = 0; i < sizeof(int) * 8; i++) | |
//cout << ((unsigned int)(1 << (sizeof(int) * 8 - i - 1)) & n ? "1" : "0"); | |
__asm push 0; // i | |
start: | |
__asm cmp dword ptr[esp], 31; | |
__asm jg out; // i <= 31 | |
__asm mov eax, 1; // eax = 1 | |
__asm mov ecx, 31; | |
__asm sub ecx, dword ptr[esp]; // 31 - i | |
__asm shl eax, cl; // 1 << (31 - i) | |
__asm mov ecx, n; // ecx = n | |
__asm test ecx, eax; // ecx = n & eax | |
__asm jz zero; | |
// print 1 here | |
cout << "1"; | |
__asm jmp loop | |
zero: | |
//print 0 here | |
cout << "0"; | |
loop: | |
__asm inc dword ptr[esp]; // i++ | |
__asm jmp start | |
out: | |
__asm add esp, 4 // remove i | |
cout << endl; | |
while (true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment