hexdump -C 实现

int macdbg_dmphex( const char* buff, int len );
int macdbg_dmphex( const char* buff, int len )
{
    int retval = 0; 
    int x, y, tot, lineoff;
    const char* curr;
    lineoff = 0;
    curr = buff;
    tot = 0;
    char pre_16bytes[16]={0x00};
    int dump_line = 0x00;

    memset(pre_16bytes, 0xff, sizeof(pre_16bytes));

    at_uart_log_string( "\nbuff = 0x%08x.\n", buff );  
               
    for( x = 0; len > x+16; ){             
         if( memcmp(pre_16bytes, curr, 16)==0x00 && curr!=buff ){
              if( dump_line==0x00  ){  
                  at_uart_log_string("0x%08x:  ", lineoff); 
                 at_uart_log_string("*    \n");      
                 dump_line = 0x01;
              }
             tot = tot + 16;
             goto skip_next;
         }
         dump_line = 0x00;
         at_uart_log_string("0x%08x:  ", lineoff); 
         for( y = 0; y < 16; y++ ){
              at_uart_log_string("%02x ", (unsigned char)*(curr + y));
         }
         at_uart_log_string("  |");
         for( y = 0; y < 16; y++ ){
              char c;
              c = *(curr + y);
              if( c > 31 && c < 127 ){
                  at_uart_log_string("%c", c);
              }else{
                  at_uart_log_string("%c", '.');
              }
              tot++;
         }
         at_uart_log_string("|\n");
         
         skip_next:
         
         memcpy(pre_16bytes, curr, 16);
         curr += 16;
         x += 16;
         lineoff+=16;
    }
                  
    //do last line
    //Ser_Printf("tot %d.\r\n", tot );
    //Ser_Printf("len %d.\r\n", len );
    
    if( tot < len ){
        curr = (buff + tot);
        at_uart_log_string("0x%08x:  ", lineoff);
        for( y = 0; y < (len - tot); y++ ){
             at_uart_log_string("%02x ", (unsigned char)*(curr + y));
        }
        //padding with spaces
        //Ser_Printf("(len - tot) %d.\r\n", (len - tot) );
        if( (len - tot) < 16 ){
            for( y = 0; y < (32 - ((len - tot)*2)); y++ ){
                 at_uart_log_string(" ");
            }
        }
        for( y = 0; y < 16-(len - tot); y++ ){
             at_uart_log_string(" ");
        }
        at_uart_log_string("  |"); 
       //Ser_Printf("(len - tot) %d.\r\n", (len - tot) );
        for( y = 0; y < (len - tot); y++ ){
            char c;
            c = *(curr + y);
            if( c >31 && c < 127 ){
                at_uart_log_string("%c", c);
            }else{
                at_uart_log_string("%c", '.');
            }
        }
    }
    at_uart_log_string("|\n");
    
    return retval;
}


//寄存器名  汇编名  功能描述                  调用返回后其值是否保持不变
//x0        zero    零寄存器                            未定义
//x1        ra      返回地址                            否
//x2        sp      栈指针                              是
//x3        gp      全局指针                            未定义
//x4        tp      线程指针                            未定义
//x5        t0      临时寄存器,或者用作替代链接寄存器   否
//x6        t1      临时寄存器                          否
//x7        t2      临时寄存器                          否
//x8        s0/fp   该寄存器需要被调函数予以保存        是
//                  或也可用作调用栈的帧指针                          
//x9        s1      该寄存器需要被调函数予以保存        是
//x10-x11   a0-a1   函数参数或返回值                    否
//x12-x17   a2-a7   函数参数                            否
//x18-x27   s2-s11  该寄存器需要被调函数予以保存        是
//x28-x31   t3-t6   临时寄存器                          否


unsigned char g_debug_buff[512] = {0x00};

static inline int a_ll(volatile int *p)
{
    int v;
    __asm__ __volatile__ ("lw %0, (%1)" : "=&r"(v) : "r"(p));
    //lw    s4,8(s2)
    return v;
}

//at_handle_HELLO_cmd_set
unsigned int get_sp( void );
unsigned int get_sp( void )
{
    unsigned int sp_reg;
    __asm__ __volatile__( "mv %0, sp" : "=r"(sp_reg) ); 

    __asm__ __volatile__("nop");
    __asm__ __volatile__("nop");
    __asm__ __volatile__("nop");

    //__asm__ __volatile__("l.li    a0,0x1070634");
    //__asm__ __volatile__("li    a0,0x1070635");
    //__asm__ __volatile__("lui    a0,0x1234");     

    //memset( g_debug_buff, 0x00, sizeof(g_debug_buff) );
    //at_uart_log_string( "g_debug_buff = %p\n", g_debug_buff );
    
    //__asm__ __volatile__("mv t3, sp");
    //__asm__ __volatile__("la sp, g_debug_buff");


    __asm__ __volatile__("la t0, g_debug_buff");
    __asm__ __volatile__("l.li    t4,0x11223344");
    
    //__asm__ __volatile__("sw  t4, 0x00(t0)");

    //at_uart_log_string( "g_debug_buffx = %#x\n",  a_ll((volatile int *)g_debug_buff) );
    return sp_reg;
}


 

相关推荐

  1. hexdump -C 实现

    2024-07-17 03:34:02       24 阅读
  2. C++实现一些C#接口

    2024-07-17 03:34:02       51 阅读
  3. CC++队列实现

    2024-07-17 03:34:02       54 阅读
  4. C/C++】C语言实现顺序表

    2024-07-17 03:34:02       29 阅读
  5. C/C++】C语言实现

    2024-07-17 03:34:02       27 阅读
  6. C/C++】C语言实现顺序栈

    2024-07-17 03:34:02       30 阅读
  7. C/C++】C语言实现std::move

    2024-07-17 03:34:02       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 03:34:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 03:34:02       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 03:34:02       58 阅读
  4. Python语言-面向对象

    2024-07-17 03:34:02       69 阅读

热门阅读

  1. 掌握Conda包管理:精通版本控制的艺术

    2024-07-17 03:34:02       20 阅读
  2. 【ubuntu】没有声音??连不上网络???

    2024-07-17 03:34:02       17 阅读
  3. bat 设置防火墙指定ip范围 指定时间段放行访问

    2024-07-17 03:34:02       18 阅读
  4. 微信小程序实现省市区级联选择组件

    2024-07-17 03:34:02       20 阅读
  5. Linux硬件中断(IRQ)的基础知识

    2024-07-17 03:34:02       19 阅读
  6. AI问答-供应链管理:WMS / 仓储管理

    2024-07-17 03:34:02       19 阅读