x86-64

Exit critical region

僤鯓⒐⒋嵵緔 提交于 2019-12-10 20:28:53
问题 Consider several threads executing concurrently the following code: long gf = 0;// global variable or class member //... if (InterlockedCompareExchange(&gf, 1, 0)==0) // lock cmpxchg { // some exclusive code - must not execute in concurrent gf = 0; // this is ok ? or need //InterlockedExchange(&gf, 0); // [lock] xchg } Treat the code above as C-like pseudo-code, which will be translated more-or-less directly into assembly without the usual concessions to compiler optimizations such as re

Are scaled-index addressing modes a good idea?

落爺英雄遲暮 提交于 2019-12-10 19:27:11
问题 Consider the following code: void foo(int* __restrict__ a) { int i; int val = 0; for (i = 0; i < 100; i++) { val = 2 * i; a[i] = val; } } This complies (with maximum optimization but no unrolling or vectorization) into... GCC 7.2: foo(int*): xor eax, eax .L2: mov DWORD PTR [rdi], eax add eax, 2 add rdi, 4 cmp eax, 200 jne .L2 rep ret clang 5.0: foo(int*): # @foo(int*) xor eax, eax .LBB0_1: # =>This Inner Loop Header: Depth=1 mov dword ptr [rdi + 2*rax], eax add rax, 2 cmp rax, 200 jne .LBB0_1

Building .so with recursive function in it

≯℡__Kan透↙ 提交于 2019-12-10 18:51:05
问题 During working on some project, I faced the issue that I can't build so library. I have got the error like: relocation R_X86_64_PC32 against symbol '' can not be used when making a shared object; recompile with -fPIC Eventually I managed to find the root cause. And it was recursive function in the library. For example, I have the following well know example: .section .text .globl factorial .type factorial,STT_FUNC factorial: push %rbp mov %rsp,%rbp mov 16(%rbp),%rax cmp $1,%rax je end

Can CAS fail for all threads?

非 Y 不嫁゛ 提交于 2019-12-10 18:39:04
问题 I'm reading about [ lock cmpxchg description]) https://www.felixcloutier.com/x86/CMPXCHG.html): This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. To simplify the interface to the processor’s bus, the destination operand receives a write cycle without regard to the result of the comparison. The destination operand is written back if the comparison fails; otherwise, the source operand is written into the destination. (The processor never

macOS 64-bit System Call Table [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-10 17:17:43
问题 This question already has an answer here : basic assembly not working on Mac (x86_64+Lion)? (1 answer) Closed 5 months ago . I can find a Linux 64-bit system call table, but the call numbers do not work on macOS - I get a Bus Error: 10 whenever I try to use them. What are the macOS call numbers for operations like sys_write ? 回答1: You can get the list of system call numbers from user mode in (/usr/include/)sys/syscall.h. The numbers ARE NOT the same as in Linux. The file is autogenerated

Writing a C function from given x86 assembly

◇◆丶佛笑我妖孽 提交于 2019-12-10 17:11:58
问题 I'm trying to reverse engineer this mystery function. This function returns an integer and takes a struct node as an argument #include "mystery.h" int mystery(struct e4_struct *s){} The header file is a simple struct declaration struct my_struct { int a; int b; }; The assembly I'm trying to reverse engineer is 400596: 8b 07 mov (%rdi),%eax 400598: 8d 04 40 lea (%rax,%rax,2),%eax 40059b: 89 07 mov %eax,(%rdi) 40059d: 83 47 04 07 addl $0x7,0x4(%rdi) 4005a1: c3 retq So far I think the function

Moving 64-bit constant to memory in x86 Assembly

不羁的心 提交于 2019-12-10 16:45:32
问题 I'm working with Intel x64 assembly, NASM compiler, trying to move the "0x4000000000000000" constant to memory, which in the ieee 754 standard double should be equal to 2.0. The code I'm using is: %define two 0x4000000000000000 section .text foo: push rbp mov rbp, rsp mov QWORD [rdi], two pop rbp ret Compiling this throws warning: signed dword immediate exceeds bounds. When i print the value in C++ it shows "0" instead of "2". I've already found a way of getting the right value, which is: mov

Jumps for a JIT (x86_64) [duplicate]

馋奶兔 提交于 2019-12-10 16:42:30
问题 This question already has answers here : Call an absolute pointer in x86 machine code (2 answers) Closed 8 months ago . I'm writing a JIT compiler in C for x86_64 linux. Currently the idea is to generate some bytecode in a buffer of executable memory (e.g. obtained with an mmap call) and jump to it with a function pointer. I'd like to be able to link multiple blocks of executable memory together such that they can jump between each other using only native instructions. Ideally, the C-level

Can I use rsp as a general purpose register

你说的曾经没有我的故事 提交于 2019-12-10 15:57:08
问题 I was told if I use rsp as a general purpose register the operating system may dump registers to where it points in the case of an interrupt, causing problematic behavior. Is this true, and if not hence, if I don't need a stack, could I use rsp as a general purpose register? Edit: Running in user space. 回答1: Aren't you screwed if an interrupt occurs? Those of you who have programmed in DOS are likely squirming at this point about the possibility of interrupts. Ordinarily, reusing the stack

what does movzbl(%rdi, %rcx, 1), %ecx mean in x86-64 assembly?

人走茶凉 提交于 2019-12-10 15:25:56
问题 I thiiink I understand that movzbl (%rdi, %rcx, 1) , %ecx means "move zero-extended byte to long" and is saying to extend ecx into 32 bits, but I'm not entirely sure what the syntax (%rdi, %rcx, 1) refers to. I've seen somewhere that that syntax refers to (Base, Index, Scale) but I can't find any resources that say what that means exactly. I'm guessing it means to move whatever info is at (%rdi, %rcx, 1) to %ecx so that (long) %ecx = (%rdi, %rcx, 1) but how do I figure out what location that