如何用嵌入式程序集解决这一系列的字符串文字错误?

问题描述

我正在编译一些包含这段代码代码

__asm__ __volatile__ ("
    pushw %%es
    movw %%ds,%%ax
    movw %%ax,%%es
    xorl %%eax,%%eax
    movl SurfBufD,%%edi
    xorl %%ebx,%%ebx
Blank2:
    movl SurfaceX,%%ecx
    rep
    stosw
    addl Temp1,%%edi
    subl SurfaceX,%%edi
    addl $1,%%ebx
    cmpl SurfaceY,%%ebx
    jne Blank2
    popw %%es
" : : : "cc","memory","eax","ebx","ecx","edi");

当我尝试编译它时,我得到了:

linux/sdlink.c:948:24: 警告:缺少终止符 " 字符 asm volatile (" ^ linux/sdlink.c:948:2: 错误: 缺少终止符 " 字符 asm volatile (" ^ linux/sdlink.c:949:3:错误:“pushw”之前的预期字符串文字 pushw %%es ^ linux/sdlink.c:966:51: 警告: 缺少终止符 " 字符 " : : : "cc","edi"); ^ linux/sdlink.c:966:2: 错误:缺少终止“字符 " : : "cc","edi");

我试图解决它,将此代码更改为:

    __asm__ __volatile__ ( 
    "pushw %%es"
    "movw %%ds,%%ax"
    "movw %%ax,%%es"
    "xorl %%eax,%%eax"
    "movl SurfBufD,%%edi"
    "xorl %%ebx,%%ebx"
"Blank2:"
    "movl SurfaceX,%%ecx"
    "rep"
    "stosw"
    "addl Temp1,%%edi"
    "subl SurfaceX,%%edi"
    "addl $1,%%ebx"
    "cmpl SurfaceY,%%ebx"
    "jne Blank2"
    "popw %%es"
" : : : cc,memory,eax,ebx,ecx,edi");

基本上,我假设这个函数希望每一行都是字符串文字,但它没有改变任何东西。那么,我必须做什么?

解决方法

  • C 中的一个字符串文字不能写成多行,但连续的字符串文字会被合并。
  • 行尾中的 \ 也表示该行应与下一行连接并视为一行,因此您可以使用它。
  • 字符串中应该有换行符。换行符可以表示为 \n

连续的字符串文字:

    __asm__ __volatile__ ( 
    "pushw %%es\n"
    "movw %%ds,%%ax\n"
    "movw %%ax,%%es\n"
    "xorl %%eax,%%eax\n"
    "movl SurfBufD,%%edi\n"
    "xorl %%ebx,%%ebx\n"
"Blank2:\n"
    "movl SurfaceX,%%ecx\n"
    "rep\n"
    "stosw\n"
    "addl Temp1,%%edi\n"
    "subl SurfaceX,%%edi\n"
    "addl $1,%%ebx\n"
    "cmpl SurfaceY,%%ebx\n"
    "jne Blank2\n"
    "popw %%es\n"
: : : "cc","memory","eax","ebx","ecx","edi");

通过 \ 连接的线路:

    __asm__ __volatile__ ( 
    "pushw %%es\n\
    movw %%ds,%%ax\n\
    movw %%ax,%%es\n\
    xorl %%eax,%%eax\n\
    movl SurfBufD,%%edi\n\
    xorl %%ebx,%%ebx\n\
Blank2:\n\
    movl SurfaceX,%%ecx\n\
    rep\n\
    stosw\n\
    addl Temp1,%%edi\n\
    subl SurfaceX,%%edi\n\
    addl $1,%%ebx\n\
    cmpl SurfaceY,%%ebx\n\
    jne Blank2\n\
    popw %%es\n"
: : : "cc","edi");