Skip to content

Instantly share code, notes, and snippets.

@Riketta
Created April 19, 2016 10:15
Show Gist options
  • Save Riketta/afe016ad9c46eaec9051f29c17092198 to your computer and use it in GitHub Desktop.
Save Riketta/afe016ad9c46eaec9051f29c17092198 to your computer and use it in GitHub Desktop.
dec2bin throw inline assembly
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
cout << "Enter dec nubmer:" << endl;
int n = 0;
cin >> n;
cout << "Entered: " << 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