[bits 32]
global _start
section .data
str_hello db "HelloWorld", 0xa
str_hello_length db $-str_hello
section .text
_start:
mov ebx, 1 ; stdout file descriptor
mov ecx, str_hello ; pointer to string of characters that will be displayed
mov edx, [str_hello_length] ; count outputs Relative addressing
mov eax, 4 ; sys_write
int 0x80 ; linux kernel system call
mov ebx, 0 ; exit status zero
mov eax, 1 ; sys_exit
int 0x80 ; linux kernel system call
Run Code Online (Sandbox Code Playgroud)
这里的基本要点是我需要将hello字符串的长度传递给linux的sys_write系统调用.现在,我很清楚我可以使用EQU,它会工作正常,但我真的想了解这里发生了什么.
所以,基本上当我使用EQU时,它会加载值,这很好.
str_hello_length equ $-str_hello …Run Code Online (Sandbox Code Playgroud) 首先,如果有人知道标准 C 库的一个函数,该函数无需查找二进制零即可打印字符串,但需要绘制字符数,请告诉我!
否则,我有这个问题:
void printStringWithLength(char *str_ptr, int n_chars){
asm("mov 4, %rax");//Function number (write)
asm("mov 1, %rbx");//File descriptor (stdout)
asm("mov $str_ptr, %rcx");
asm("mov $n_chars, %rdx");
asm("int 0x80");
return;
}
Run Code Online (Sandbox Code Playgroud)
GCC 将以下错误告知“int”指令:
"Error: operand size mismatch for 'int'"
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我这个问题吗?
我正在尝试编写一个Python脚本来测试我在汇编中针对预期输出编写的各种代码的输出.但是我很难将输出重定向到文件中.我写了以下内容:
extern printf
LINUX equ 80H ; interupt number for entering Linux kernel
EXIT equ 1 ; Linux system call 1 i.e. exit ()
section .data
intfmt: db "%ld", 10, 0
segment .text
global main
main:
push rax
push rsi
push rdi
mov rsi, 10
mov rdi, intfmt
xor rax, rax
call printf
pop rdi
pop rsi
pop rax
call os_return ; return to operating system
os_return:
mov rax, EXIT ; Linux system call 1 i.e. exit ()
mov rbx, …Run Code Online (Sandbox Code Playgroud) ; NASM
push 30 ; '0'
mov rax, 4 ; write
mov rbx, 1 ; stdout
mov rcx, rsp ; ptr to character on stack
mov rdx, 1 ; length of string = 1
int 80h
Run Code Online (Sandbox Code Playgroud)
上面的代码不会向标准输出打印任何内容。当我给它一个 ptr 给section .data. 我究竟做错了什么?
我正在使用elf64编译并尝试获取参数并将其写入控制台.
我将函数称为./test wooop
在单步执行gdb之后似乎没有问题,一切都设置好了:
rax:0x4 rbx:0x1 rcx:指向字符串,x/6cb $ rcx给出'w''o''o''o''p'0x0 rdx:0x5 <---正确确定长度
在int 80h之后,rax包含-14并且没有任何内容打印到控制台.如果我在.data中定义一个字符串,它就可以了.gdb以相同的方式显示$ rcx的值.
有任何想法吗?这是我的完整来源
%define LF 0Ah
%define stdout 1
%define sys_exit 1
%define sys_write 4
global _start
section .data
usagemsg: db "test {string}",LF,0
testmsg: db "wooop",0
section .text
_start:
pop rcx ;this is argc
cmp rcx, 2 ;one argument
jne usage
pop rcx
pop rcx ; argument now in rcx
test rcx,rcx
jz usage
;mov rcx, testmsg ;<-----uncomment this to print ok!
call print
jmp exit
usage:
mov …Run Code Online (Sandbox Code Playgroud) 我正在编译 32 位二进制文件,但想在其中嵌入一些 64 位程序集。
void method() {
asm("...64 bit assembly...");
}
Run Code Online (Sandbox Code Playgroud)
当然,当我编译时,我会收到有关引用错误寄存器的错误,因为寄存器是 64 位的。
evil.c:92: Error: bad register name `%rax'
Run Code Online (Sandbox Code Playgroud)
是否可以添加一些注释,以便 gcc 将使用 64 位汇编器来处理 asm 部分。我有一个单独编译的解决方法,使用 PROT_EXEC|PROT_WRITE 映射到页面中,然后复制到我的代码中,但这非常尴尬。
以下代码使用rsi寄存器作为循环计数器打印hello world 10次.
section .data
hello: db 'Hello world!',10
helloLen: equ $-hello
section .text
global _start
_start:
mov rsi, 0 ;<--- use r8 here
do_loop:
inc rsi ;<--- use r8 here
;print hello world
mov eax,4
mov ebx,1
mov ecx,hello
mov edx,helloLen
int 80h
cmp rsi, 10 ;<--- use r8 here
jnz do_loop
;system exit
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h;
Run Code Online (Sandbox Code Playgroud)
如果我试图使用r8寄存器而不是rsi作为循环计数器,则会导致无限循环.这里的r8寄存器只是一个例子.它也适用于寄存器r9,r10.
有人可以解释一下,因为我认为这些都是通用寄存器,你应该被允许使用它们吗?
我编写了一个汇编程序来显示遵循 AT&T 语法的数字的阶乘。但它不起作用。这是我的代码
.text
.globl _start
_start:
movq $5,%rcx
movq $5,%rax
Repeat: #function to calculate factorial
decq %rcx
cmp $0,%rcx
je print
imul %rcx,%rax
cmp $1,%rcx
jne Repeat
# Now result of factorial stored in rax
print:
xorq %rsi, %rsi
# function to print integer result digit by digit by pushing in
#stack
loop:
movq $0, %rdx
movq $10, %rbx
divq %rbx
addq $48, %rdx
pushq %rdx
incq %rsi
cmpq $0, %rax
jz next
jmp loop
next:
cmpq $0, %rsi …Run Code Online (Sandbox Code Playgroud) 在16位x86汇编中你会写:
mov ax, @DATA
mov ds, ax
Run Code Online (Sandbox Code Playgroud)
32位的extact代码转换如何?我不知道32位的数据段.
mov eax, @DATA
mov ? , eax
Run Code Online (Sandbox Code Playgroud)
谢谢您的回答!
因为我对汇编很新,所以如果用户在汇编中输入大写字母或反之亦然,我有一些关于如何从小写转换为大写的问题.这是我到目前为止:
section .data
Enter db "Enter: "
Enter_Len equ $-Enter
Output db "Output: "
Output_Len equ $-Output
Thanks db "Thanks!"
Thanks_Len equ $-Thanks
Loop_Iter dd 0 ; Loop counter
section .bss
In_Buffer resb 2
In_Buffer_Len equ $-In_Buffer
section .text
global _start
_start:
; Print Enter message
mov eax, 4 ; sys_write
mov ebx, 1
mov ecx, Enter
mov edx, Enter_Len
int 80h
; Read input
mov eax, 3 ; sys_read
mov ebx, 0
mov ecx, In_Buffer
mov edx, In_Buffer_Len
int …Run Code Online (Sandbox Code Playgroud)