Nasm 打印到下一行 [英] Nasm print to next line
问题描述
我有以下用 nasm Assembly 编写的程序:
section .text全局_start:_开始:;输入变量mov edx, inLenmov ecx, inMsgmov ebx, 1移动轴,4整数 0x80mov edx, 2mov ecx, num1移动 ebx, 0移动轴,3整数 0x80mov edx, inLenmov ecx, inMsgmov ebx, 1移动轴,4整数 0x80mov edx, 2mov ecx, num2移动 ebx, 0移动轴,3整数 0x80;将输入值放入正确的寄存器中mov eax, [num1]子 eax, '0' ;将字符转换为数字mov ebx, [num2]子 ebx, '0' ;将字符转换为数字;执行加法添加 eax, ebx添加 eax, '0' ;将数字转换为字符;在 res 中设置总和mov [res], eax;输出结果mov edx, resLenmov ecx, resMsgmov ebx, 1移动轴,4整数 0x80mov edx, 1mov ecx, resmov ebx, 1移动轴,4整数 0x80;退出程序移动轴,1整数 0x80.bss 节num1 resb 2num2 resb 2resb 2.data 节inMsg db "输入数字:", 0xA, 0xDinLen 等于 $-inMsgresMsg db "结果:", 0xA, 0xDresLen 等于 $-resMsg
当我运行它时,控制台看起来像这样:
tyler@ubuntu:~/ASM/Addition$ ./Add输入编号:3输入编号:2结果:5tyler@ubuntu:~/ASM/Addition$
我怎样才能得到它,这样 5 将在它自己的行上打印,而不是在它之后直接打印 cmd?IE.它应该是这样的:
tyler@ubuntu:~/ASM/Addition$ ./Add输入编号:3输入编号:2结果:5tyler@ubuntu:~/ASM/Addition$
您已经掌握了所有信息,只是还没有看到:
resMsg db "结果:", 0xA, 0xD
你知道这到底是什么意思吗?它定义了一个由以下字符组成的字符串:
结果:XY
...其中 X
和 Y
实际上是不可见字符(数值为 0xA=10 和 0xD=13,也称为换行 (LF) 和回车return (CR)) 导致输出换行.由于它们的不可见性,它们被指定在双引号之外 - 您不能仅仅将它们包含在双引号中,因此您必须写下它们的数值.
当然你也可以单独使用它们:
newLineMsg db 0xA, 0xDnewLineLen equ $-newLineMsg
(newLineLen
当然会是 2,但我把它留在这里是为了让系统和你目前使用的一样,以便于理解.)
所以只输出一个没有任何其他文本的换行符(你想在 5
之后做什么),你可以使用:
mov edx, newLineLenmov ecx, newLineMsgmov ebx, 1移动轴,4整数 0x80
...就像 resMsg
/resLen
一样.
但是,正如 Jester 所指出的,在 Linux 上,您还应该能够仅输出一个换行符 (0xA)(并且还可以从代码中删除现有的 0xD
).>
I have the following program written in 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? I.E. it should look like this:
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. 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.)
So to just output a line break without any other text (what you want to do after the 5
), you can then use:
mov edx, newLineLen
mov ecx, newLineMsg
mov ebx, 1
mov eax, 4
int 0x80
...just like with 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).
这篇关于Nasm 打印到下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!