c - 在汇编中实现 wc

标签 c assembly

<分区>

我正在尝试将典型的 wc C 代码转换为 intel 汇编代码。 C代码是源码,汇编是我做的。 由于它没有任何函数调用,我认为这会起作用,但它总是 最终 iCharCount 损坏并且 iLineCount 保持为 0。可能是什么问题?

C 代码

#include <stdio.h>
#include <ctype.h>
enum {FALSE, TRUE};
int main(void){
   int iLineCount = 0;
   int iWordCount = 0;
   int iCharCount = 0;
   int iChar;
   int iInWord = FALSE;
   iChar = fgetc(stdin);
   for( ;iChar != EOF; ){
      iCharCount++;

      if (iChar == '\n') iLineCount++;
      if (isspace(iChar)){
         if (iInWord){
            iWordCount++;
            iInWord = FALSE;
         }
      }
      else if (!iInWord) iInWord = TRUE;
      iChar = fgetc(stdin);
   }

   if (iInWord) iWordCount++;
   printf("%7d%8d%8d\n", iLineCount, iWordCount, iCharCount);
   return 0;
}

汇编代码

    .file "wc.s"
    .section        .rodata
    .equ TRUE, 1
    .equ FALSE, 0
    .equ SPACE, 32
    .equ LCHANGE, 10
    .equ TAB, 9
    .equ EOF, -1
    .globl main
    .type main, @function
main:
start:
    pushl %ebp
    movl %esp, %ebp
    andl $-16, %esp
    subl $48, %esp
    movl $0, 20(%esp) #int iLineCount = 0
    movl $0, 24(%esp) #int iWordCount
    movl $0, 28(%esp) #int iCharCount
    movl $0, 36(%esp) #int iInWord = FALSE
    movl stdin, %eax
    movl %eax, (%esp)
    call fgetc
    movl %eax, 32(%esp) #iChar = fgetc(stdin)
    cmpl $EOF, 8(%ebp) #Start of for( ;iChar != EOF;)
    je endloop
loop:
    incl 28(%ebp)   #iCharCount++
    cmpl $LCHANGE, 32(%ebp) #if(iChar == '\n')
    jne ecomp1
    incl 20(%esp) #iLineCount++
ecomp1:
    cmpl $LCHANGE, 32(%esp) #if(isspace(iChar) ->'\n'
    je isspace
    cmpl $SPACE, 32(%esp) #-> ' '
    je isspace
    cmpl $TAB, 32(%esp) # -> '\t'
    je isspace
    jmp elsespace
isspace:
    cmpl $TRUE, 36(%esp)    #if(iInWord)
    jne last
    movl $FALSE, 36(%esp) #iInWord = FALSE
    incl 24(%esp)   #iWordCount++
    jmp last
elsespace:
    cmpl $FALSE, 36(%esp)   #if(!InWord)
    jne last
    movl $TRUE, 36(%esp)    #iInWord = TRUE
last:
    movl stdin, %eax
    call fgetc
    movl %eax, 32(%esp)     #iChar = fgetc(stdin)
    cmpl $EOF, 32(%esp)     #Recheck for statement
    jne loop
endloop:
    cmpl $TRUE, 36(%esp)    #if(iInWord)
    jne else
    incl 24(%esp)   #iWordCount++
else:
    movl 28(%esp), %eax
    movl %eax, 12(%esp) #push iCharCount in printf
    movl 24(%esp), %eax
    movl %eax, 8(%esp) #push iWordCount in printf
    movl 20(%ebp), %eax
    movl %eax, 4(%esp) #push iLineCount in printf
    movl $.format, (%esp)
    call printf
    leave
    ret
.format:
    .string "%7d%8d%8d\n"
    .text

最佳答案

您的 assembly list 中缺少行:标签 last以及 iChar = fgetc(stdin); 都丢失了在循环结束时。

您应该简化您的 C 代码。先用经典成语:

while ((iChar = fgetc(stdin)) != EOF) { ... }

不需要测试

else if (!iInWord) iInWord = TRUE;

这里也不需要 2 个测试:

  if (isspace(iChar)){
     if (iInWord){
        iWordCount++;
        iInWord = FALSE;
     }
  }

你可以这样简化循环和单词测试:

while ((iChar = fgetc(stdin)) != EOF) {
    iCharCount++;
    if (iChar == '\n') iLineCount++;
    if (isspace(iChar)) {
        iWordCount += iInWord;
        iInWord = 0;
    } else {
        iInWord = 1;
    }
}
iWordCount += iInWord;

printf格式不合适:如果计数变得太大,数字将粘在一起。改用这个:

printf("%7d %7d %7d\n", iLineCount, iWordCount, iCharCount);

至于汇编版本,好像是编译器生成的,然后有点手工打补丁。你确实重写了 isspace() test 并且您不测试一些空白字符,例如 '\r''\f' . '\n' 的测试是多余的,你可以跳转到isspace:递增后 iLineCount .

编辑:您的汇编代码的问题是:您有时会错误地使用 %ebp而不是 %esp访问局部变量。

    ...
    cmpl $EOF, 8(%ebp) #Start of for( ;iChar != EOF;)
    ...
loop:
    incl 28(%ebp)   #iCharCount++
    cmpl $LCHANGE, 32(%ebp) #if(iChar == '\n')
    ...
    movl 20(%ebp), %eax
    ...            

关于c - 在汇编中实现 wc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474028/

相关文章:

检查硬盘上的坏扇区

assembly - UEFI 是如何工作的?

assembly - 堆栈大小的使用上限是否有限制?

c - 如何在多引导头文件中正确使用 QEMU

c - 使用两个结构C编程的单链表插入函数

c++ - 程序运行时是否受对象在内存中的驻留位置影响?

带无效数据成员问题的 C 链表/堆栈

assembly - 麻烦理解汇编命令“加载有效地址”

c - 包含在内联汇编中

c++ - 使用带有填充零的 printf 按顺序打印数字