Original Post
First, let me say this is a homework assignment, but I have an excuse why I need help!!
In my computer organization and architecture class, we are learning AT&T assembly syntax. I asked the prof. for permission to use x86, so I could use visual studio. He said ok, but now I have no book or anything on the syntax, and 90% of the links on assembly are broken, or do not have the answer to my question.
The programming assignment is very simple. We input a number using c++, and then with asm, we shl and call the putchar function to output the number in binary. In AT&T syntax, when you shift the bits, they go into the carry flag, so that''s what I have been trying. It''s not working, I''m getting all 0''s. Here''s the code.
#include <iostream>
#include <cstdio>
using namespace std;
int main(void)
{
unsigned int num, bits = 32;
cout << "Enter a number to convert to binary" << endl;
cin >> num;
__asm
{
pusha
mov eax, num
mov ebx, 1
top:
shl eax, 1 /* shift one bit to the left */
jc carry /* if carry flag = one, jump to carry label */
push ''0'' /* push character ''0'' on the stack */
call putchar /* call putchar */
add esp, 4 /* clear the stack */
inc ebx /* increment ebx */
cmp ebx, bits /* if ebx is greater than the number of bits */
jg done /* go to done label */
jmp top /* if not, go to top and repeat */
carry:
push ''1'' /* push character ''1'' on the stack */
call putchar /* call putchar */
add esp, 4 /* clear stack */
inc ebx /* increment ebx */
cmp ebx, bits /* if ebx is greater than the number of bits */
jg done /* go to done label */
jmp top /* if not, go to top and repeat */
done:
popa
}
cout << endl;
return 0;
}