gpt4 book ai didi

linux - 带有用户输出的 NASM 代码,然后输入到参数中。 (代码错误)

转载 作者:太空宇宙 更新时间:2023-11-04 10:59:54 25 4
gpt4 key购买 nike

我在使用以下代码实现用户输出和参数时遇到问题。

一个 BASH 示例,我想在 NASM 中实现一个类似的示例函数。

read -p "Enter the group you would like to create: " newgroup
groupadd $newgroup

The reason why I am creating this code is to show a comparison of different languages, used within Linux, This is one part of my research project. So i know, this can be done in other languages e.g. Python BASH and C etc. Before anyone asks.

我相信我在正确的地方,这是下面的代码。

;---------------------------NSAM----------------------
section .data ; initialized data, can be var's
userg: db "Type in the user group to add: ",10,0 ; 1st string, 10 is used to drop down a line. 0 is used to end the line.
userg_L: equ $-userg ; string length, automatically updates how long the string is, so you don't have to type 10 etc. every time its changed
respns: db "groupadded: ",10,0 ; 2nd string, 10 is used to drop down a line. 0 is used to end the line.
respns_L equ $-respns ; 2nd string length, automatically updates how long the string is, so you don't have to type 10 etc. every time its changed
command: db '/bin/groupadd', 0h ; command to execute.
arg1: db userg, 0h ; Shows all users on the system.
argu: dd command
dd arg1 ; arguments passed to the command line.
dd 0h ; struct.
enviro: dd 0h ; no arguments are need for the environment variables.

;========================================================
section .bss ; uninitialised data, also can be var's
userg_V resb 255 ; reserves 255 bytes of data.

;========================================================

section .text ; Asm core code. any of the sections can be in any order.
global _start: ; Makes _start the global function

_start: ; Makes _start the global function
mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write)
mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal
mov ecx, userg ; contains the label of the string.
mov edx, userg_L ; this is the length of the string.
int 80h ; calls the Linux kernel

mov eax, 0x03 ; 3 is the code to read user input. e.g. kernel function (sys_read)
mov ebx, 0x00 ; This is the error code memory block.
mov ecx, userg_V ; reserves 255 bytes of data name of string.
mov edx, 255 ; reserves 255 bytes of data.
int 80h ; calls the Linux kernel.

mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write)
mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal
mov ecx, respns ; calls the var
mov edx, respns_L ; calls the userg var length
int 80h ; calls the Linux kernel.

mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write)
mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal
mov ecx, userg_V ; reserves 255 bytes of data name of string.
mov edx, 255 ; reserves 255 bytes of data.
int 80h ; calls the Linux kernel.

mov edx, enviro ; Environment variables
mov ecx, argu ; Arguments to pass to the command line
mov ebx, command ; address to execute
mov eax, 11 ; SYS_EXECVE kernel opcode (11)
int 80h
call exit ; Calls the exit function.


;---------------------------------ASM exit code below!-------------------------------------------
exit:
mov eax, 0x01 ; [EAX] is 1 this is the exit code! e.g. = kernel function (sys_exit)
mov ebx, 0x00 ; This is the error code memory block.
int 80h ; calls the Linux kernel.

错误是:

ld -s -o myasm myasm.o
myasm.o: In function `arg1':
myasm.asm:(.data+0x3e): relocation truncated to fit: R_386_8 against `.data'

此错误已使用 dd 修复,但不会将用户组添加到系统。

下面的代码可以显示所有用户,但不会将用户输入作为参数。这是工作命令代码的示例,但没有用户输入。那么如何使用命令行代码实现用户输入。

command:         db      '/bin/cat', 0h    ; command to execute.
arg1: db '/etc/passwd', 0h ; Shows all users on the system.

最佳答案

这是固定版本。请注意,groupadd 可能不在 /bin 中,如果 exec 失败,您应该打印一条错误消息。

;---------------------------NASM----------------------
section .data ; initialized data, can be var's
userg: db "Type in the user group to add: ",10 ; 1st string, 10 is used to drop down a line.
userg_L: equ $-userg ; string length, automatically updates how long the string is, so you don't have to type 10 etc. every time its changed
respns: db "groupadded: ",10 ; 2nd string, 10 is used to drop down a line. 0 is used to end the line.
respns_L equ $-respns ; 2nd string length, automatically updates how long the string is, so you don't have to type 10 etc. every time its changed
command: db '/bin/groupadd', 0h ; command to execute.
argu: dd command
dd userg_V ; arguments passed to the command line.
dd 0h ; struct.
enviro: dd 0h ; no arguments are need for the environment variables.

;========================================================
section .bss ; uninitialised data, also can be var's
userg_V resb 255 ; reserves 255 bytes of data.

;========================================================

section .text ; Asm core code. any of the sections can be in any order.
global _start: ; Makes _start the global function

_start: ; Makes _start the global function
mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write)
mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal
mov ecx, userg ; contains the label of the string.
mov edx, userg_L ; this is the length of the string.
int 80h ; calls the Linux kernel

mov eax, 0x03 ; 3 is the code to read user input. e.g. kernel function (sys_read)
mov ebx, 0x00 ; This is the error code memory block.
mov ecx, userg_V ; reserves 255 bytes of data name of string.
mov edx, 255 ; reserves 255 bytes of data.
int 80h ; calls the Linux kernel.
push eax ; save length of input

mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write)
mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal
mov ecx, respns ; calls the var
mov edx, respns_L ; calls the userg var length
int 80h ; calls the Linux kernel.

mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write)
mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal
mov ecx, userg_V ; the input text
mov edx, [esp] ; the length of the input text
int 80h ; calls the Linux kernel.

pop eax ; get back input length
mov [eax + userg_V - 1], byte 0 ; chop off trailing newline
mov edx, enviro ; Environment variables
mov ecx, argu ; Arguments to pass to the command line
mov ebx, command ; address to execute
mov eax, 11 ; SYS_EXECVE kernel opcode (11)
int 80h

;---------------------------------ASM exit code below!-------------------------------------------
exit:
mov eax, 0x01 ; [EAX] is 1 this is the exit code! e.g. = kernel function (sys_exit)
mov ebx, 0x00 ; This is the error code memory block.
int 80h ; calls the Linux kernel.

关于linux - 带有用户输出的 NASM 代码,然后输入到参数中。 (代码错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27384874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com