如何更改汇编中的保留字节数?

问题描述 投票:0回答:1

我刚开始学习汇编,我想知道如何更改保留字节数,如果可能的话。

我创建了以下代码,它将根据底部的

max
变量创建一个长度递减的直角三角形。但是,为了确保我不保留多余的字节,我想计算它(我相信它适用于
calculateBytes
),然后保留这些字节。

如果这不可能,请解释。如果没有,是否还有其他方法可以帮助完成这项工作?

bits 64

global  _start

section .text
    _start:     
        mov        rdx, output
        mov        r8, max
        mov        r9, r8
        mov        r10, r8
        mov        r11, r8

    calculateBytes:
        add        r11, r10
        dec        r10
        jnz        calculateBytes

    line:
        mov        byte [rdx], '*'
        inc        rdx
        dec        r9
        
        jnz        line

    lineDone:
        mov        byte [rdx], 10
        inc        rdx
        dec        r8
        mov        r9, r8
        jnz        line
        
    done:
        mov        rax, 1 ; initiate writing
        mov        rdi, 1 ; to stdout
        mov        rsi, output; use output
        mov        rdx, size ; with this size
        syscall
        mov        rax, 60
        xor        rdi, rdi
        syscall

section .bss
    size      equ     1000
    output    resb    size
    max       equ     30
assembly nasm
1个回答
0
投票
calculateBytes:
    add        r11, r8
    dec        r10
    jnz        calculateBytes

这会产生太多的

max * (max + 1)

你想要的是(第一个数字是星号,第二个数字是换行符):

(30 + 1) + (29 + 1) + (28 + 1) ... (2 + 1) + (1 + 1)

装配中:

calculateBytes:
    add        r11, r10
    dec        r10
    jnz        calculateBytes

    ...

    mov        rdx, r11
    syscall
© www.soinside.com 2019 - 2024. All rights reserved.