LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CPU register test (https://www.linuxquestions.org/questions/programming-9/cpu-register-test-369263/)

arunka 10-03-2005 02:28 AM

CPU register test
 
hI ALL
can any one sugest how to verify that CPU registers are working properly in C under linux..
like i need to check and make sure the the registers of my CPU are working fine??

Thanks in advance..

Arun

Dark_Helmet 10-03-2005 03:56 AM

Under C? No. You would need to write assembly code for that. C does provide a "register" storage class for variables. However, it does not allow the programmer to specify which register, and it does not guarantee the register request will be granted. Even if C allowed that, it would only be able to test general purpose registers. There would be no direct tests for the program counter, error status, or other special purpose registers.

If you're just worried that something might be wrong, don't. If a register in the processor was broken or misbehaving, I seriously doubt your computer would boot at all. I mean, we're talking near-impossible to have a functional computer with a bad register.

Brian Knoblauch 10-03-2005 09:09 AM

If you had a bad register, you would not be able to get anywhere at all. If you still feel the need to test them, the best way (simplest) is to write some assembly code for real mode, boot into DOS and do it that way. You won't be able to test all of them.

Dark_Helmet 10-03-2005 10:03 AM

Quote:

In an email from arunka
i think that we can embeded the assembly and C using asm() function...
Through this can we have our register test performed..
Do u have any idea of how we can use the asm().

Maybe the point I was trying to make isn't completely clear. The entire functional code would need to be in assembly to do this test. Not some, not most, but all of it. It doesn't make any sense to wrap it in C. It would be the same thing as using C with nothing but system() calls to implement a shell script. It just doesn't make sense to use C. If you have the knowledge to write the assembly, then just do it in assembly... forget about C.

And to reiterate the other point, if you have a broken register, you will not have a functional computer, which implies you wouldn't be able to compile the program in the first place.

Trust me, you would have serious problems with a broken register.

Lastly, according to the reference book I have (Programmer's Reference: C/C++ Second Edition), the asm keyword is supported only by C++. The book includes the C99 standard, so that leads me to believe it's still current. If gcc supports the asm keyword in standard C, and you are absolutely intent on doing this, then you'll need to find some documentation on what format gcc requires for assembly instructions.

Brian Knoblauch 10-03-2005 10:27 AM

If I was going to do it in C, I'd just create an executable string which would have the machine code in it. Pretty silly though. C is just the wrong tool, like using a screwdriver to drill a hole in the wall (yeah, I've done that) :)

primo 10-03-2005 11:47 PM

Here's some code I wrote in 1999. You may do anything with it.

I still don't know what you're trying to do. It would be easy to make a function that returns the value of any register. It wouldn't be portable though. The asm keyword may be used too. Just browse some crypto code, ie openssl, gnupg, libgcrypt, mcrypt, libmcrypt, et cetera

compile with "gcc showreg.s"

Code:

#
# showreg
#  Shows the contents of 32-bit registers
#
# (!c) 1999
#

        .section        .rodata
EAX:
        .string "EAX = %0.8X\n"
EBX:
        .string "EBX = %0.8X\n"
ECX:
        .string "ECX = %0.8X\n"
EDX:
        .string "EDX = %0.8X\n"
ESI:
        .string "ESI = %0.8X\n"
EDI:
        .string "EDI = %0.8X\n"
EBP:
        .string "EBP = %0.8X\n"
ESP:
        .string "ESP = %0.8X\n"
EIP:
        .string "EIP = %0.8X\n"
CS:
        .string " CS = %0.4X\n"
DS:
        .string " DS = %0.4X\n"
ES:
        .string " ES = %0.4X\n"
FS:
        .string " FS = %0.4X\n"
GS:
        .string " GS = %0.4X\n"
SS:
        .string " SS = %0.4X\n"

        .text
        .align 4

.globl main
        .type    main,@function
main:
        pushl %eax
        pushl $EAX
        call printf

        pushl %ebx
        pushl $EBX
        call printf

        pushl %ecx
        pushl $ECX
        call printf

        pushl %edx
        pushl $EDX
        call printf

        pushl %esi
        pushl $ESI
        call printf

        pushl %edi
        pushl $EDI
        call printf

        pushl %ebp
        pushl $EBP
        call printf

        addl $56, %esp  # Resetear %esp por 14 pushl's

        pushl %esp
        pushl $ESP
        call printf

        xorl %eax, %eax # movl $0, %eax
        movw %cs, %ax
        pushl %eax
        pushl $CS
        call printf

        xorl %eax, %eax
        movw %ds, %ax
        pushl %eax
        pushl $DS
        call printf

        xorl %eax, %eax
        movw %es, %ax
        pushl %eax
        pushl $ES
        call printf

        xorl %eax, %eax
        movw %fs, %ax
        pushl %eax
        pushl $FS
        call printf

        xorl %eax, %eax
        movw %gs, %ax
        pushl %eax
        pushl $GS
        call printf

        xorl %eax, %eax
        movw %ss, %ax
        pushl %eax
        pushl $SS
        call printf

        addl $56, %esp  # Resetear %esp

        call dummy      # pushl %eip
 dummy: pushl $EIP
        call printf
        addl $8, %esp

        xorl %eax, %eax
        ret



All times are GMT -5. The time now is 05:29 AM.