簡體   English   中英

nasm打印到下一行

[英]Nasm print to next line

我用nasm Assembly編寫了以下程序:

section .text
    global _start:

_start:
    ; Input variables
    mov edx, inLen
    mov ecx, inMsg
    mov ebx, 1
    mov eax, 4
    int 0x80

    mov edx, 2
    mov ecx, num1
    mov ebx, 0
    mov eax, 3
    int 0x80

    mov edx, inLen
    mov ecx, inMsg
    mov ebx, 1
    mov eax, 4
    int 0x80

    mov edx, 2
    mov ecx, num2
    mov ebx, 0
    mov eax, 3
    int 0x80

    ; Put input values in correct registers
    mov eax, [num1]
    sub eax, '0'    ; convert char to num
    mov ebx, [num2]
    sub ebx, '0'    ; convert char to num

    ; Perform addition
    add eax, ebx
    add eax, '0'    ; convert num to char

    ; Set sum in res
    mov [res], eax

    ; Output result
    mov edx, resLen
    mov ecx, resMsg
    mov ebx, 1
    mov eax, 4
    int 0x80

    mov edx, 1
    mov ecx, res
    mov ebx, 1
    mov eax, 4
    int 0x80

    ; Exit program
    mov eax, 1
    int 0x80

    section .bss
    num1    resb 2
    num2    resb 2
    res resb 2

section .data
    inMsg db "Input number: ", 0xA, 0xD
    inLen equ $-inMsg
    resMsg db "Result: ", 0xA, 0xD
    resLen equ $-resMsg

當我運行它時,控制台看起來像這樣:

tyler@ubuntu:~/ASM/Addition$ ./Add 
Input number: 
3
Input number: 
2
Result: 
5tyler@ubuntu:~/ASM/Addition$ 

我如何獲得它,以便5可以在自己的行上打印,而不能在其后直接打印cmd? IE瀏覽器應該看起來像這樣:

tyler@ubuntu:~/ASM/Addition$ ./Add 
Input number: 
3
Input number: 
2
Result: 
5
tyler@ubuntu:~/ASM/Addition$ 

您已經有了所有信息,只是看不到:

resMsg db "Result: ", 0xA, 0xD

你知道這到底意味着什么嗎? 它定義了一個由以下字符組成的字符串:

Result: XY

...其中XY實際上是不可見的字符(數值0xA = 10和0xD = 13,也稱為換行(LF)和回車(CR)),它們導致輸出換行到新行。 由於它們不可見,因此在雙引號之外指定了它們-您不能僅在其中將它們包括在內,因此必須編寫其數值。

但是當然您也可以單獨使用它們:

newLineMsg db 0xA, 0xD
newLineLen equ $-newLineMsg

newLineLen當然是2,但為了使您更容易理解,我在這里將其保留為與您當前使用的系統相同。)

因此,僅輸出不包含任何其他文本的換行符(您要在5之后執行的操作),則可以使用:

mov edx, newLineLen
mov ecx, newLineMsg
mov ebx, 1
mov eax, 4
int 0x80

...就像使用resMsg / resLen


但是,正如Jester指出的那樣,在Linux上,您還應該只能輸出單個換行(0xA)(並且還可以從代碼中刪除現有的0xD )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM