read adds extra line at the end
I'm learning assmebler by myself at the moment and I finally managed to read input from the terminal and calculate with it.
I use sys_read for that and it works perfectly fine but when I use it the terminal acts like I pressed enter after runnning the program (one line with root@kali:~/ASM$
). This does not happen when using scanf.
Here is my code:
sys_read equ 3 sys_write equ 4 stdout equ 1 stdin equ 2 section .data prompt db "Enter two 1-digit numbers for an integer division.", 10, 0 result db 10, "%i / %i = %i.", 10, 0 section .bss a resb 4 b resb 4 c resb 4 section .text extern printf global main main: push ebp mov ebp, esp push ebx push esi push edi push prompt call printf mov eax, sys_read mov ebx, stdin mov ecx, a mov edx, 1 int 80h sub dword [a], 0x30 mov eax, sys_read mov ebx, stdin mov ecx, b mov edx, 1 int 80h mov eax, sys_read mov ebx, stdin mov ecx, b mov edx, 1 int 80h sub dword [b], 0x30 mov dx, 0 mov ax, [a] div dword [b] mov [c], ax push dword [c] push dword [b] push dword [a] push result call printf add esp, 40 pop edi pop esi pop ebx mov esp, ebp pop ebp ret
And here is the output I get:
root@kali:~/ASM$ ./div Enter two 1-digit numbers for an integer division. 1 1 1 / 1 = 1. root@kali:~/ASM$ root@kali:~/ASM$
I don't understand why this extra line appears.
Your result data contains an extra newline character ( 10
) before the string to be printed:
result db 10, "%i / %i = %i.", 10, 0
^^
Removing this will remove the extra newline in the output.
You can remove the extra shell line by consuming more characters from stdin, either by flushing the buffer, or by reading more than three characters from the buffer. The newline that you press to enter the data will also be stored in the line's buffer, and, if not read by your code, will be read by the shell as an empty line. (ie it will exhibit the same behavior as the shell would if you just pressed enter in the shell without typing anything else.)
Also, I would advise using extended 32-bit registers for the math instead of 16-bit registers, since your variables are 32-bit.
链接地址: http://www.djcxy.com/p/15906.html上一篇: 在可编辑的iframe中插入固定的元素
下一篇: 阅读增加额外的行