gpt4 book ai didi

c - 在汇编中实现 wc

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:21 28 4
gpt4 key购买 nike

<分区>

我正在尝试将典型的 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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com