在汇编中接受用户输入?简单程序(在 macOS 上)
Accepting user input in assembly? Simple program (On macOS)
我在使用这个接受用户名称并打印 "Hello, name here"
的简单程序时遇到了一些问题
到目前为止,这是我的代码...
%define SYSCALL_WRITE 0x2000004
%define SYSCALL_EXIT 0x2000001
%define SYSCALL_READ 0x2000003
SECTION .data
prompt db "Enter name "
text2 db "Hello, "
SECTION .bss
name resb 16
SECTION .text
global _start
_start:
call _printText1
call _getInput
call _printText2
call _printName
mov rax, SYSCALL_EXIT
mov rdi, 0
syscall
_printText1:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, prompt
mov rdx, 11
syscall
ret
_getInput:
mov rax, SYSCALL_READ
mov rdi, 0
mov rsi, name
mov rdx, 1
syscall
ret
_printText2:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, text2
mov rdx, 7
syscall
ret
_printName:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, name
mov rdx, 16
syscall
ret
当我执行它时,输出不打印 "Hello, "。输入名称的第一个字母打印在下一个命令行之前...
nMy-MacBook:Assembly username$ ame
名称的其余部分被接受为命令参数,系统对此回复
-bash: ame: command not found
我到底做错了什么?我删除了 _getInput 和 _printName 函数,它仍然只打印 "Enter name " 而没有打印 "Hello, ".
谢谢。
在汇编中你必须考虑到每一点;尤其是在做输入输出的时候。
下面是您为 RDX 编辑的部分代码,其中我选择了一些任意长度。
最后一行显示命令行程序执行、提示、用户输入,然后是最终输出。
; reference:
;
; lsb_release -a
; Distributor ID: Ubuntu
; Description: Ubuntu 16.04.3 LTS
; Release: 16.04
; Codename: xenial
; assemble and link
; nasm -f elf64 -g -F dwarf srsrso_001.s -o srsrso_001.o && ld srsrso_001.o -o srsrso_001
;%define SYSCALL_WRITE 0x2000004
;%define SYSCALL_EXIT 0x2000001
;%define SYSCALL_READ 0x2000003
SECTION .data
prompt db "Enter name "
text2 db "Hello, ",0xa,0
SYSCALL_WRITE equ 1
SYSCALL_EXIT equ 60
SYSCALL_READ equ 0
SECTION .bss
name resb 0xff ; some length
SECTION .text
global _start
_start:
call _printText1
call _getInput
call _printText2
call _printName
mov rax, 60
mov rdi, 0
syscall
_printText1:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, prompt
mov rdx, 11
syscall
ret
_getInput:
mov rax, SYSCALL_READ
mov rdi, 0
mov rsi, name
mov rdx, 0xff ; some length
syscall
ret
_printText2:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, text2
mov rdx, 7
syscall
ret
_printName:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, name
mov rdx, 0xff ; some length
syscall
ret
示例输出:
$ ./srsrso_001
Enter name David John Lewis Benjamen Kyle Smith-Wenson
Hello, David John Lewis Benjamen Kyle Smith-Wenson
我在使用这个接受用户名称并打印 "Hello, name here"
的简单程序时遇到了一些问题到目前为止,这是我的代码...
%define SYSCALL_WRITE 0x2000004
%define SYSCALL_EXIT 0x2000001
%define SYSCALL_READ 0x2000003
SECTION .data
prompt db "Enter name "
text2 db "Hello, "
SECTION .bss
name resb 16
SECTION .text
global _start
_start:
call _printText1
call _getInput
call _printText2
call _printName
mov rax, SYSCALL_EXIT
mov rdi, 0
syscall
_printText1:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, prompt
mov rdx, 11
syscall
ret
_getInput:
mov rax, SYSCALL_READ
mov rdi, 0
mov rsi, name
mov rdx, 1
syscall
ret
_printText2:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, text2
mov rdx, 7
syscall
ret
_printName:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, name
mov rdx, 16
syscall
ret
当我执行它时,输出不打印 "Hello, "。输入名称的第一个字母打印在下一个命令行之前...
nMy-MacBook:Assembly username$ ame
名称的其余部分被接受为命令参数,系统对此回复
-bash: ame: command not found
我到底做错了什么?我删除了 _getInput 和 _printName 函数,它仍然只打印 "Enter name " 而没有打印 "Hello, ".
谢谢。
在汇编中你必须考虑到每一点;尤其是在做输入输出的时候。
下面是您为 RDX 编辑的部分代码,其中我选择了一些任意长度。
最后一行显示命令行程序执行、提示、用户输入,然后是最终输出。
; reference:
;
; lsb_release -a
; Distributor ID: Ubuntu
; Description: Ubuntu 16.04.3 LTS
; Release: 16.04
; Codename: xenial
; assemble and link
; nasm -f elf64 -g -F dwarf srsrso_001.s -o srsrso_001.o && ld srsrso_001.o -o srsrso_001
;%define SYSCALL_WRITE 0x2000004
;%define SYSCALL_EXIT 0x2000001
;%define SYSCALL_READ 0x2000003
SECTION .data
prompt db "Enter name "
text2 db "Hello, ",0xa,0
SYSCALL_WRITE equ 1
SYSCALL_EXIT equ 60
SYSCALL_READ equ 0
SECTION .bss
name resb 0xff ; some length
SECTION .text
global _start
_start:
call _printText1
call _getInput
call _printText2
call _printName
mov rax, 60
mov rdi, 0
syscall
_printText1:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, prompt
mov rdx, 11
syscall
ret
_getInput:
mov rax, SYSCALL_READ
mov rdi, 0
mov rsi, name
mov rdx, 0xff ; some length
syscall
ret
_printText2:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, text2
mov rdx, 7
syscall
ret
_printName:
mov rax, SYSCALL_WRITE
mov rdi, 1
mov rsi, name
mov rdx, 0xff ; some length
syscall
ret
示例输出:
$ ./srsrso_001
Enter name David John Lewis Benjamen Kyle Smith-Wenson
Hello, David John Lewis Benjamen Kyle Smith-Wenson