x86-64

Import constants in x86 with gas

≯℡__Kan透↙ 提交于 2021-01-29 18:25:48
问题 I have the following two files in assembly: # file.s .globl _start _start: mov $10, %edi mov $SYS_EXIT, %eax syscall # utils.s SYS_EXIT = 60 SYS_WRITE = 1 SYS_STDOUT = 1 What is required to be able to link these two files into an executable. To assemble and link I've tried doing: $ as file.s -o file.o $ as utils.s -o utils.o $ ld utils.o file.o -o file # file.o: In function `_start': # (.text+0x8): undefined reference to `SYS_EXIT' Which seems to just mean I'm not properly importing the file

Whay rip is used here in a Hello world assembly? [duplicate]

孤街醉人 提交于 2021-01-29 14:11:34
问题 This question already has answers here : Why is the address of static variables relative to the Instruction Pointer? (1 answer) Why are global variables in x86-64 accessed relative to the instruction pointer? (2 answers) Closed 12 months ago . I found some assembly code about "hello world", but I don't understand leaq L1(%rip), %rdi, why rip is used here? .text .globl _main _main: pushq %rbp movq %rsp, %rbp leaq L1(%rip), %rdi <--it's the first time that I found IP is directly used in code.

Trying to disable paging through cr0 register

三世轮回 提交于 2021-01-29 11:11:54
问题 I'm trying to disable paging completely with an LKM (don't ask me why I'm just experimenting). I've tried just changing the value directly with the LKM. void disable_paging(void) { asm("movq %cr0, %rax\n\t" "movq $0xFFFFFFFEFFFFFFFF, %rbx\n\t" "and %rbx, %rax\n\t" "movq %rax, %cr0\n\t"); } Well the expected result would be the bit being flipped. The actual result is a segfault. 回答1: TL:DR: This can't work, but your attempt didn't disable paging because you cleared bit 32 instead of bit 31.

How to call a function in an external assembly file [duplicate]

試著忘記壹切 提交于 2021-01-29 05:38:07
问题 This question already has answers here : calling assembly function from c (4 answers) Calling an assembly function from C (2 answers) Passing parameters from C to GNU Assembly function in 64bit (1 answer) Closed 6 days ago . I am trying to go from a C function to a function with inline-asm to a standalone function in an assembly file. Here is what I have so far: #include <stdio.h> int add_five(int n) { n = n + 5; return n; } int add_five_inline(int n) { asm("lea 5(%1), %0" : "=r" (n) : "r" (n

How are segment registers involved in memory address translation?

孤街醉人 提交于 2021-01-29 05:29:58
问题 In what I've learned so far about segmentation: A virtual address contains a segment selector and an offset The segment selector is used in conjunction with the GDTR to find the linear address of the segment descriptor The segment descriptor houses information regarding the chosen segment, including its linear address So, my questions are: Based on what I've read, the virtual address is loaded into the segment register, and then somehow the translation is continued from there. What happens to

Assembly - Moving through a register/array with an offset of 5

放肆的年华 提交于 2021-01-29 05:20:53
问题 Quick question. This code will not compile: mov eax, dword [rbx+rsi*5] I don't expect it to, with the explaination that mov and multiplication are two different CPU operations. The only reason it can be achieved is through bit-shifting. However, this does compile: mov eax, dword [lst+rsi*5] With "lst" being a variable array. It also produces output when used in context (so the code compiles AND runs). What's the explanation for why this works? yasm -Worphan-labels -g dwarf2 -f elf64 NAME.asm

Passing parameters to a function — push or registers [duplicate]

半腔热情 提交于 2021-01-29 05:14:00
问题 This question already has answers here : What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64 (4 answers) Where is the x86-64 System V ABI documented? (3 answers) Why does Windows64 use a different calling convention from all other OSes on x86-64? (4 answers) Closed 5 months ago . I want to write a function that does something like: def addme(x, y): return x + y I know there is already an instruction for this, but I'm practicing how to

Linux Assembly x86_64 create a file using command line parameters

限于喜欢 提交于 2021-01-29 05:00:59
问题 I'm trying to teach myself assembly. I've found a good website; however, everything is written for x86 and I use a 64-bit machine. I know what the problem is, but I don't know how to fix it. If I run the program with strace, then here is the results: execve("./file", ["./file", "hello"], [/* 94 vars */]) = 0 creat(NULL, 0) = -1 EINVAL (Invalid argument) write(0, NULL, 0 <unfinished ...> +++ exited with 234 +++ So, I know that when I call creat , that the file name "hello" is not being passed

How do I use labels in address calculations

你离开我真会死。 提交于 2021-01-29 02:05:37
问题 I have the following snippet of inline assembly: procedure Foo; asm //..... @partialloop: shr rdx,1 //every instruction is 4 bytes lea r11,[rip + (7*4)+(@partial-@offset)] @offset: sub r11,rdx xor r8,r8 jmp r11 //do a partial loop @loop64: mov [rcx],rax @partial: mov [rcx+08H],rax //.... end; The compiler does not accept this syntax: E2105 Inline assembler syntax error Create use of offset , ptr or @@ does not help. What syntax do I need to use to make this compile? 回答1: Label addresses are

Problems with non-atomic access example on GNU website

99封情书 提交于 2021-01-29 01:34:36
问题 On the website of GNU there is a simple example available which is supposed to demonstrate the problems appearing with non-atomic access. The example contains a small mistake, they have forgotten #include <unistd.h> : #include <signal.h> #include <stdio.h> #include <unistd.h> struct two_words { int a, b; } memory; static struct two_words zeros = { 0, 0 }, ones = { 1, 1 }; void handler(int signum) { printf ("%d,%d\n", memory.a, memory.b); alarm (1); } int main (void) { signal (SIGALRM, handler