Okay, so I am learning Assembly and I started with 32bit and bswap seemed to work fine.

Now I have this code

main.c

#include <stdio.h>
#include <stdlib.h>

extern int asm_proc(void);

void print_bits(unsigned long long bits) {

    for (int i = 0; i < sizeof(unsigned long long) * 8; i += 1) {

        printf("%i", (bits & 0x0000000000000001 ? 1 : 0));
        bits >>= 1;
        if((i+1) % 8 == 0) printf("\n");
    }
    printf("\n");
}

int main() {

    print_bits(asm_proc());

    return 0;
}

main.asm

BITS 64

section .text

global asm_proc

asm_proc:
    mov rax, 01h
    cpuid
    mov rax, 0ffffffffffffffffh
    bswap rax
    ret

but the bswap just sets rax to 0x0000000000000000.

Any help would be much appriciated. Thanks :)

Okay. After rewriting my code a few times and examining, I found the problem!

The return type here: extern int asm_proc(void); should have been long long (or just long on most compilers). I'm not sure exactly why this cause the probalems it did, but it is fixed now.

Yes, on both 32 and 64-bit systems, usually an int is 32-bits, but in your 64-bit system, you need a 64-bit integer, which is a long in the 64-bit system, and long long in a 32-bit one. The long long distinction is currently irrelevant on 64-bit systems, but that may change to a 128 bit value in the future, so caveat (beware) programmer!

My system is a 64bit system. However I am using the MinGWx64 compiler on Windows 8.1 and apparently the sizes are off. I have since decided to use the stdint.h and inttypes.h headers.

Thanks for your reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.