[英]Nasm print to next line
I have the following program written in nasm Assembly: 我用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
When I run it the console looks like this: 当我运行它时,控制台看起来像这样:
tyler@ubuntu:~/ASM/Addition$ ./Add
Input number:
3
Input number:
2
Result:
5tyler@ubuntu:~/ASM/Addition$
How can I get it so the 5 will print on its own line and not have the cmd print directly after it? 我如何获得它,以便5可以在自己的行上打印,而不能在其后直接打印cmd? IE it should look like this:
IE浏览器应该看起来像这样:
tyler@ubuntu:~/ASM/Addition$ ./Add
Input number:
3
Input number:
2
Result:
5
tyler@ubuntu:~/ASM/Addition$
You have all the info already, you just don't see it yet: 您已经有了所有信息,只是看不到:
resMsg db "Result: ", 0xA, 0xD
Do you know what this means exactly? 你知道这到底意味着什么吗? It defines a string made of the following characters:
它定义了一个由以下字符组成的字符串:
Result: XY
...where X
and Y
are actually invisible characters (with numerical values 0xA=10 and 0xD=13, also known as line feed (LF) and carriage return (CR)) which cause the output to wrap to a new line. ...其中
X
和Y
实际上是不可见的字符(数值0xA = 10和0xD = 13,也称为换行(LF)和回车(CR)),它们导致输出换行到新行。 They are specified outside of the doublequotes because of their invisible nature - you can't just include them there so you have to write their numerical values instead. 由于它们不可见,因此在双引号之外指定了它们-您不能仅在其中将它们包括在内,因此必须编写其数值。
But of course you can use them alone as well: 但是当然您也可以单独使用它们:
newLineMsg db 0xA, 0xD
newLineLen equ $-newLineMsg
( newLineLen
will be 2 of course but I left it here to keep the system the same as you currently use, for easier understanding.) (
newLineLen
当然是2,但为了使您更容易理解,我在这里将其保留为与您当前使用的系统相同。)
So to just output a line break without any other text (what you want to do after the 5
), you can then use: 因此,仅输出不包含任何其他文本的换行符(您要在
5
之后执行的操作),则可以使用:
mov edx, newLineLen
mov ecx, newLineMsg
mov ebx, 1
mov eax, 4
int 0x80
...just like with resMsg
/ resLen
. ...就像使用
resMsg
/ resLen
。
However, as Jester pointed out, on Linux you should also be able to output just a single line feed (0xA) (and also remove the existing 0xD
's from your code). 但是,正如Jester指出的那样,在Linux上,您还应该只能输出单个换行(0xA)(并且还可以从代码中删除现有的
0xD
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.