uuuuuuuuu 发表于 2021-6-10 02:27:32

求助求助

用汇编语言完成一个字符串统计,要求分别出字母,数字,其他字符的个数

人造人 发表于 2021-6-10 09:35:44

      .file   "main.c"
      .text
      .section      .rodata
      .align 8
.LC0:
      .string "letter: %lu, number: %lu, other: %lu\n"
      .text
      .globlmain
      .type   main, @function
main:
.LFB0:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset 6, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register 6
      subq    $32, %rsp
      movq    $0, -24(%rbp)
      movq    $0, -16(%rbp)
      movq    $0, -8(%rbp)
      jmp   .L2
.L5:
      call    __ctype_b_loc@PLT
      movq    (%rax), %rdx
      movl    -28(%rbp), %eax
      cltq
      addq    %rax, %rax
      addq    %rdx, %rax
      movzwl(%rax), %eax
      movzwl%ax, %eax
      andl    $1024, %eax
      testl   %eax, %eax
      je      .L3
      addq    $1, -24(%rbp)
      jmp   .L2
.L3:
      call    __ctype_b_loc@PLT
      movq    (%rax), %rdx
      movl    -28(%rbp), %eax
      cltq
      addq    %rax, %rax
      addq    %rdx, %rax
      movzwl(%rax), %eax
      movzwl%ax, %eax
      andl    $2048, %eax
      testl   %eax, %eax
      je      .L4
      addq    $1, -16(%rbp)
      jmp   .L2
.L4:
      addq    $1, -8(%rbp)
.L2:
      call    getchar@PLT
      movl    %eax, -28(%rbp)
      cmpl    $-1, -28(%rbp)
      jne   .L5
      movq    -8(%rbp), %rcx
      movq    -16(%rbp), %rdx
      movq    -24(%rbp), %rax
      movq    %rax, %rsi
      leaq    .LC0(%rip), %rax
      movq    %rax, %rdi
      movl    $0, %eax
      call    printf@PLT
      movl    $0, %eax
      leave
      .cfi_def_cfa 7, 8
      ret
      .cfi_endproc
.LFE0:
      .size   main, .-main
      .ident"GCC: (GNU) 11.1.0"
      .section      .note.GNU-stack,"",@progbits

人造人 发表于 2021-6-10 09:36:31

这是对应的C语言代码
#include <stdio.h>
#include <ctype.h>

int main(void) {
    size_t letter = 0;
    size_t number = 0;
    size_t other = 0;
    int ch;
    while((ch = getchar()) != EOF) {
      if(isalpha(ch)) ++letter;
      else if(isdigit(ch)) ++number;
      else ++other;
    }
    printf("letter: %lu, number: %lu, other: %lu\n", letter, number, other);
    return 0;
}
页: [1]
查看完整版本: 求助求助