尝试将正数除以负数。
目前我的程序将正确划分,并且我可以正确访问其余部分。
但是当我输入一个正数除以一个负值时,它根本不会除。
我知道有一种方法可以“签名扩展”并且它会正确划分。我只是看不懂说明书
movsss $imm/%reg %reg/mem 移动,符号扩展 231 movzss $imm/%reg %reg/mem 移动,零扩展
当然那是 att 语法,我需要 intel 语法
这是我的代码
xor rdx, rdx
mov rax, [input]
mov rcx, [input2]
idiv rcx
Run Code Online (Sandbox Code Playgroud)
想法如何除以负数?
我正在学习汇编,我创建了一个简单的退出程序。
.section __DATA, __data
.section __TEXT, __text
.globl _main
_main:
movl $0x2000001, %eax #System call exit, offset by 0x00000
movl $1, %ebx #Exit Return code
syscall #Wakes up kernal to run the systen call
Run Code Online (Sandbox Code Playgroud)
运行文件后如何回显 $?返回 0
组装和链接命令:
as exit.asm -o exit.o
ld exit.o -e _main -o exit
./exit
Run Code Online (Sandbox Code Playgroud) 最近,我一直在从《从头开始编程》一书中学习 x86 汇编,但我有一台 x86-64 计算机,所以事情在某一时刻开始出错(在本书的早期)。我谈到了处理函数的部分,特别是电源示例。在此示例中,他将参数压入堆栈,然后将它们复制到函数后面的寄存器中。他的代码如下:
pushl $3 # second argument
pushl $2 # first argument
call power # call function
...
power:
pushl %ebp # save old base pointer
movl %esp, %ebp # make stack pointer the base pointer
subl $4, %esp # get room for our local storage
movl 8(%ebp), %ebx # put first argument in %eax
movl 12(%ebp), %ecx # put second argument in %ecx
Run Code Online (Sandbox Code Playgroud)
当然,这是32位的,而我运行的是64位,所以我尝试更新寄存器和指令后缀,最终得到这样的结果(这次不需要注释):
pushq $3
pushq $2
call power
...
power: …Run Code Online (Sandbox Code Playgroud) 我正在阅读 Go 源代码,正如人们所做的那样,当我阅读该fastrand()函数时(对于我的机器来说该函数将位于文件中asm_amd64.s),我遇到了以下代码片段:
XORL $0x88888eef, DX
CMOVLMI BX, DX
MOVL DX, m_fastrand(AX)
Run Code Online (Sandbox Code Playgroud)
对于我的一生,我无法弄清楚CMOVLMI应该做什么。对它的搜索表明,似乎只有 Go 知道它的任何事情;CMOVxx我可以找到AMD X86_64 参考中定义的大量操作码,并且维基百科页面有条件移动指令的悠久历史,但这并没有出现在该列表中的任何位置。
CMOVLMI 在哪里定义的?它是 Go 内部汇编器所独有的吗?
我很困惑为什么如果你想在堆栈上分配 8 字节存储,你使用以下命令
subq $8,%rsp
Run Code Online (Sandbox Code Playgroud)
%rsp 存储一个地址。为什么从 %rsp 中存储的地址中减去文字值 8 会分配 8 个字节?特别是,为什么$x对应x个字节?
编辑:我的问题在此stackoverflow 回复中得到了解答。它以字节为单位,因为这是 int 的大小,而 int 是传入的文字参数。
编辑2:以上是不正确的。参见博的评论。
这是一个简单的 NASM 64 位 linux 汇编程序:
_exit:
mov rax, 60
mov rdi, 0
syscall
Run Code Online (Sandbox Code Playgroud)
我的电脑是 AMD(x86_64 64 位),我知道这个汇编程序也可以在 INTEL 64 位处理器上运行。
但我有以下这些问题!
https://www.cs.fsu.edu/~langley/CNT5605/2017-Summer/assembly-example/assembly.html
我看到如下示例。但是我没有找到系统调用的手册。例如,60 表示退出,1 表示写入。是否有所有系统调用的完整手册(包括调用号和参数的含义)?
global _start
section .text
_start:
; ssize_t write(int fd, const void *buf, size_t count)
mov rdi,1 ; fd
mov rsi,hello_world ; buffer
mov rdx,hello_world_size ; count
mov rax,1 ; write(2)
syscall
; exit(result)
mov rdi,0 ; result
mov rax,60 ; exit(2)
syscall
hello_world: db "Hello World!",10
hello_world_size EQU $ - hello_world
Run Code Online (Sandbox Code Playgroud) 我目前被困在我的 c 项目上,我试图用 C 语言在内存中运行一个程序,但我一直都有分段错误。
首先我有一个简单的hello_worl.c,我编译它,我有输出hello_world。
第二,我hello_world像这样转换为 C 标头:
xxd -i hello_world > hello.h
(hello.h 的内容)
unsigned char hello[] = {
...
}
unsigned int hello_len = 16696;
Run Code Online (Sandbox Code Playgroud)
最后,我创建了程序main.c,看起来像这样:
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include "hello.h"
int main ()
{
int (*my_hello) () = NULL;
// allocate executable buffer
my_hello = mmap (0, sizeof(hello), PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
// copy code to buffer
memcpy (my_hello, hello, sizeof(hello));
__builtin___clear_cache (my_hello, my_hello + …Run Code Online (Sandbox Code Playgroud) 我正在尝试splash2x.raytrace在两种架构上编译一个程序(转换后的 PARSEC 基准套件包) - amd64 和 riscv64;但是,当我尝试在两者上本地编译它时,我得到了无法解释的不同行为。
具体来说,虽然在 amd64 上它编译,但在 riscv64 上它失败了很多multiple definition of....
该程序有大约十几个.c文件和一个标题,它们都包含在其中(“ rt.h”)。
样本变量是:
# File: rt.h
INT prims_in_leafs;
Run Code Online (Sandbox Code Playgroud)
导致错误:
/usr/bin/ld: cr.o:/home/riscv/parsec-benchmark/ext/splash2x/apps/raytrace/obj/riscv64-linux.gcc/rt.h:750: multiple definition of `prims_in_leafs'; bbox.o:/home/riscv/parsec-benchmark/ext/splash2x/apps/raytrace/obj/riscv64-linux.gcc/rt.h:750: first defined here
/usr/bin/ld: env.o:/home/riscv/parsec-benchmark/ext/splash2x/apps/raytrace/obj/riscv64-linux.gcc/rt.h:750: multiple definition of `prims_in_leafs'; bbox.o:/home/riscv/parsec-benchmark/ext/splash2x/apps/raytrace/obj/riscv64-linux.gcc/rt.h:750: first defined here
/usr/bin/ld: fbuf.o:/home/riscv/parsec-benchmark/ext/splash2x/apps/raytrace/obj/riscv64-linux.gcc/rt.h:750: multiple definition of `prims_in_leafs'; bbox.o:/home/riscv/parsec-benchmark/ext/splash2x/apps/raytrace/obj/riscv64-linux.gcc/rt.h:750: first defined here
/usr/bin/ld: geo.o:/home/riscv/parsec-benchmark/ext/splash2x/apps/raytrace/obj/riscv64-linux.gcc/rt.h:750: multiple definition of `prims_in_leafs'; bbox.o:/home/riscv/parsec-benchmark/ext/splash2x/apps/raytrace/obj/riscv64-linux.gcc/rt.h:750: first defined here
Run Code Online (Sandbox Code Playgroud)
用于编译的命令非常简单:
gcc -c -static-libgcc -pthread *.c
gcc *.o -pthread -o …Run Code Online (Sandbox Code Playgroud) 我有以下一段代码:
typedef struct {
int x;
int y;
int z;
int w;
} s32x4;
s32x4
f() {
s32x4 v;
v.x = 0
return v;
}
Run Code Online (Sandbox Code Playgroud)
生成(gcc -O2):
f:
xor eax, eax
xor edx, edx ; this line is questionable
ret
Run Code Online (Sandbox Code Playgroud)
其中 clang 输出(clang -O2):
f: # @f
xor eax, eax
ret
Run Code Online (Sandbox Code Playgroud)
GCC插入XOR那里?