linux nasm 程序集打印从零到 100 的所有数字

标签 linux assembly x86 nasm

我正在编写一个程序来打印从 0 到 100 的所有数字。我这样做的唯一原因是测试打印多位数字。

我遇到的问题是我的程序只打印出数字 1 和 2。我不知道为什么。我的编译器编译正常,没有错误,也没有链接器错误。

这是我的代码:

SECTION .data
len EQU 32
NUL EQU 0
countlen EQU 8

SECTION .bss
counter resb countlen
strlen resb countlen

SECTION .text
GLOBAL _start
_start:
    mov BYTE[counter], 1              ; set counter to 1
    mov BYTE[strlen], 1               ; set string length counter to 1
    mov ecx, counter                  ; move the counter to ecx
    add BYTE[ecx], NUL                ; add null terminator to ecx
    mov esi, 9                        ; move 9 to esi

Length: 
    cmp [counter], esi                ; compare counter to esi
    jle Set                           ; if equal, goto set
    inc BYTE[strlen]                  ; increment the string size
    mov eax, 10                       ; move 10 to eax
    mov ebx, esi                      ; move esi to ebx
    mul ebx                           ; multiply ebx by esi
    add eax, 9                        ; add nine to the result
    mov esi, eax                      ; move the result to esi
    jmp Length                        ; jump to Length 

Set:
    mov esi, 9                        ; reset checker

Check:
    cmp BYTE[strlen], 1               ; is it one digit?
    je Single                         ; if yes, jump to single
    cmp BYTE[strlen], 3               ; is it 100?
    je Exit                           ; if yes, jump to Exit

Print:                                 ; this section deals with multi-digit numbers                                   
    cmp BYTE[ecx], NUL                ; check if end of string
    je Exit                           ; if equal goto exit 
    mov eax, 4
    mov ebx, 1
    mov edx, 1
    int 80h                           ; print number

    inc ecx                           ; point to next digit in number
    jmp Print                         ; jump to Print

Single:                                   ; this section deals with single digit numbers         add BYTE[counter], '0'            ; convert to ASCII
    mov eax, 4                       
    mov ebx, 1
    mov ecx, counter 
    mov edx, countlen 
    int 80h                           ; print the digit
    jmp Length                        ; go back

Exit:                                     ; Exit section
    mov eax, 1                        ; sys_exit
    mov ebx, 0                        ; return 0
    int 80h                           ; syscall

为什么要这样做?另外,我需要更改什么才能使其按预期工作?

提前致谢

赖利H

更新:

编辑以包含“打印”标签

最佳答案

这是我在标准输出上打印数字的函数。它在 AT&T 抱歉 ;)

movl <your decimal here>, %eax
xor %ecx, %ecx  # the counter
movl $10, %ebx  

loop:
xor %edx, %edx
div %ebx        # isolate the last digit, remainder in edx
add $48, %dx    # '0' is 48 in ascii, result is the ascii equivalent
shl $8, %dx     # move the ascii byte to %dh
pushw %dx       # puch ascii code on the stack
inc %esp        # point to the ascii byte! (discard %dl)
inc %ecx        # count the digits
cmp $0, %eax
jnz loop

movl $4, %eax     # write()
movl $1, %ebx     # stdout
movl %ecx, %edx   # now edx holds the number of digits
movl %esp, %ecx   # load the address of string array
int $0x80         # the string array is on top of the stack

干杯!

关于linux nasm 程序集打印从零到 100 的所有数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13097906/

相关文章:

xcode - Mac 上的 x86 汇编

c++ - 现代 C++ 编译器的有效优化策略

linux - 从 txt 文件创建 Samba 用户的脚本

linux - 具有 arm64 架构的 QEMU 上的 Xen

c - 创建一个头文件来扫描整数而不使用 stdio.h(scanf)

c - 映射库函数分配的间接指针 - C - OSX

assembly - 编写跨步 x86 基准测试

assembly - 英特尔 JCC 勘误表 - 真的应该单独对待 JCC 吗?

linux - 如何从 VM 克隆 Git 存储库?

linux 在扩展前用增量字符分割文件