hello everybody! I don't know if I am allowed to post this topic here.. If not, I apologize for this in advance..
I just began learning IA32 assembly and wanted to write a simple program which is supposed to compare 2 numbers stored in reg EAX and EBX byte by byte and store the biggest in ECX. My code doesn't work when the most significant byte starts with 1. For example in the code below it doesn't work properly.. Could anybody help me to find out the mistake please?
Thanks a lot!
Code:
#include <stdlib.h>
#include <stdio.h>
unsigned max;
void printbit (unsigned n);
int main()
{
asm(
"pushl %eax\r\n"
"pushl %ebx\r\n"
"pushl %ecx\r\n" //RESULT
"pushl %edx\r\n" //COUNTER
"pushl %esi\r\n" //BIT MASK
"pushl %ebp\r\n" //STORE RESULT OF EAX AND BITMASK
"pushl %edi\r\n" //STORE RESULT OF EBX AND BITMASK
"movl $0b11111111000001010001000000000000, %eax\r\n"//INIZIALIZE EAX
"movl $0b00000000011000000000000011111111, %ebx\r\n"//INIZIALIZE EBX
"movl $0b00000000000000000000000011111111,%esi\r\n"//INIZIALIZE MASK
"xorl %ecx,%ecx\r\n"//RESET TOTAL
"xorl %edx,%edx\r\n"//RESET COUNTER
"cicle:\r\n"
"movl %eax,%ebp\r\n"
"movl %ebx,%edi\r\n"
"andl %esi,%ebp\r\n"
"andl %esi,%edi\r\n"
"cmp %ebp,%edi\r\n"
"jg use_edi\r\n"
"xorl %ebp,%ecx\r\n"
"ret:\r\n"
"cmp $3,%edx\r\n"
"je end\r\n"
"incl %edx\r\n"
"shl $8,%esi\r\n"
"jmp cicle\r\n"
"use_edi:\r\n"
"xorl %edi,%ecx\r\n"
"jmp ret\r\n"
"end:\r\n"
"movl %ecx,max\r\n"
"popl %edi\r\n"
"popl %ebp\r\n"
"popl %esi\r\n"
"popl %edx\r\n"
"popl %ecx\r\n"
"popl %ebx\r\n"
"popl %eax\r\n"
);
printbit(max);
}
void printbit (unsigned n)
{
unsigned bits=sizeof (unsigned)*8;
unsigned mask = 1<<(bits-1);
int i;
for (i=0;i<bits;i++)
{
if (mask&n)putchar('1');
else putchar('0');
mask>>=1;
}
printf("\n");
}