Write registers data into array using asm C

0

I created a program that writes registers data into variables using asm. And it seems to be working well. But then I decided to replace variables by an array and to write registers data into an array. I used the same approach, but noticed that when I'm printing variables and array members they have different values, but should have the same values. What I'm doing wrong trying to write the registers values into an array? As I understand it should work the same way if to write to a standalone variable.

void read_registers(void)
{
    int ebx_val, ecx_val, edx_val;
    char reg_name[4][4] = {"ebx", "ecx", "edx"};
    int reg_val[3];

    printk("\n===OLD VALUES BELOW===");
    test_syscall();/*inside of the syscall registers were written 0xDEADBEEF*/
    __asm__ volatile (
            "\t movl %%ebx,%0" : "=r"(ebx_val));
    __asm__ volatile (
            "\t movl %%ecx,%0" : "=r"(ecx_val));
    __asm__ volatile (
            "\t movl %%edx,%0" : "=r"(edx_val));

    printk("\nReg ebx val user mode 0x%x\n", ebx_val);
    printk("\nReg ecx val user mode 0x%x\n", ecx_val);
    printk("\nReg edx val user mode 0x%x\n", edx_val);

    printk("\n===NEW VALUES BELOW===");

    __asm__ volatile (
            "\t movl %%ebx,%0" : "=r"(reg_val[0]));
    __asm__ volatile (
            "\t movl %%ecx,%0" : "=r"(reg_val[1]));
    __asm__ volatile (
            "\t movl %%edx,%0" : "=r"(reg_val[2]));
    for(int i=0; i<3; i++)
    {
    printk("\nReg %s val is 0x%x\n", reg_name + i,
            reg_val[i]);
    }
c
gcc
assembly
inline-assembly
cpu-registers
asked on Stack Overflow Apr 7, 2020 by maxxlife • edited Apr 7, 2020 by maxxlife

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0