assembly,x86,segmentation-fault,mmap , Asm x86 segmentation fault in reading from file


Asm x86 segmentation fault in reading from file

Question:

Tag: assembly,x86,segmentation-fault,mmap

I'm receiving segmentation fault in 5th line of loop2. This is the part of my code which is reading values from mmapped file byte by byte. Could you please tell me what I'm doing wrong?

.global _start
_start:
    mov $2,%rax
    mov 16(%rsp),%rdi
    mov $02,%rsi
    syscall 

    cmp $0,%rax
    jl exit
1:
    mov %rax,%r8
    mov $9,%rax
    mov $0,%rdi
    mov $4096,%rsi
    mov $0x3,%rdx
    mov $0x1,%r10
    mov $0,%r9
    syscall

    mov $0,%r10
loop2:
    mov (%rax,%r10,1),%r9b
    cmp $32,%r9
    je 2f

    sub $48,%r9b
    mov %r9b,(%r8,%r10,1)

    inc %r10
        jmp loop2
2:
    mov %r10,%rcx
    dec %r10

Answer:

The value in R8 at the time your program crashes is the file descriptor returned by the open syscall. Its value is probably 3 which isn't a valid address. You'll need to stores these values in a range of memory you've properly allocated. You can create a buffer in your program's .bss section or dynamically allocate memory with the brk syscall.


Related:


How can I access the individual elements of an array in a loop?


assembly,mips
I need to print the cells of an array, I have an array which contains the word "HELLO_WORLD", I manage to print an index by its own but I can't manage to print all the cells one by one, here is the code : loop: la $t0, hexdigits # address...

Why can't I use compiler intrinsics in an asm block?


delphi,assembly
Is this a compiler bug? program Project44; {$APPTYPE CONSOLE} uses System.SysUtils; function Test: integer; asm xor eax,eax add eax,SizeOf(NativeInt); end; begin WriteLn(Test); //Typically outputs 31 or 49 {$ifdef CPUX86} WriteLn('should be 4'); {$else} {$ifdef CPUX64} WriteLn('should be 8'); {$endif}{$endif} ReadLn end. This program outputs all kinds of things, except for...

storing array from user and accessing it


arrays,assembly,input,user,mips
I have to create an int array based on user input, sum the integers, and output the sum and the array itself. My code calculates the sum correctly, but I cannot output the list. It only prints out the last number and zeros and then gets stuck in an infinite...

nasm: jump when input is NULL


c,assembly,nasm,libc
I am recoding puts using nasm (64bit), and when puts receives NULL as argument it prints (null). I'm trying to recreate that behaviour, except I can't get the code to jump to the part where it prints (null). Instead it just prints nothing here's my code: global _my_puts section .text...

ARM assembly cannot use immediate values and ADDS/ADCS together


gcc,assembly,arm,instructions
I am currently trying to speed up some of my C functions on a Cortex-M0 (Freescale KL25Z) using assembly. I get a problem with this minimal test program: @.syntax unified .cpu cortex-m0 .text .global test .code 16 test: mov r0, #0 adds r0, r0, #1 bx lr When I try...

Reserve bytes in stack: x86 Assembly (64 bit)


assembly,x86-64,cpu-registers
pushq %rbp movq %rsp, %rbp subq $32, %rsp I have big question regarding explanation of "$32" in third instruction. The information from search and blogs specifies that in above third instruction we are reserving certain "bytes" of space for stack. From doc, %rsp is 64 bit register and %esp is...

Is this an overflow, or maybe more keyboard data?


assembly,nasm,bootloader
I am writing a bootloader, and it's functionality is basically limited to printing a string, then copying keyboard characters to the screen as they are typed. While writing the routines that read and write the key, I noticed my print routine was not detecting the null terminator in offset (plus)...

GCC emits vastly different code using “-march=native” on similar architectures


c,gcc,assembly,sse,avx
I'm working on writing an OpenCL benchmark in C. Currently, it measures the fused multiply-accumulate performance of both a CL device, and the system's processor using C code. The results are then cross checked for accuracy. I wrote the native code to take advantage of GCC's auto vectorizer, and it...

Open Watcom Inline assembly SEG and OFFSET operators


c,assembly,memory-address,watcom
Today, I have learned that the SEG operator in MASM by default returns the address of the GROUP and not the address of the SEGMENT of the expression in question, and that there are options and methods to override that. Since I am currently doing a complex project in Open...

Wrong answer from DIV assembly


assembly,x86
I have a part of my code mov di,3 mov cx,16 looop: xor dx,dx shl bx,1 adc dx,dx cmp cx,16 je cx16 (it's will dec cx and jump loop) push dx dec cx cmp cx,0 je cx0 mov ax,cx div di cmp dx,0 jne looop when cx = 3 i...

NASM: Makefile for library


assembly,makefile,nasm
I'm having trouble building a makefile for a library in nasm, since it requires that you run nasm with one input file at a time. I have tried with the %.o : %.s thing but I'm probably doing it incorrectly since it's not working. Here is what I have: NAME...

Counter not working after jumps - assembly language


loops,assembly,counter,increment
For some reason, when i switch to mouse input switch back to keyboard input for my program, increasing and decreasing the counter has no effect. It works perfectly in the first loop where we input characters. Here is the program guys, any advice? look at whatspeed jump for reference after...

Visual Studios building and debugging .cpp file without main()


c++,assembly,visual-studio-2013
Professor just gave us a block of c++ code in order to learn about the debug windows in Visual Studio, however after creating a blank win32 console project and then dragging in the .cpp file I get the missing .exe error message when attempting to debug. I've looked everywhere and...

LC3 assembly-how to count string length


string,assembly,lc3
I am trying to create a program at LC3 assembly that counts the length of a string in the following way: All data is already stored somewhere in memory. There is a variable in which the address of the first element of the string is stored. (I apologize in advance...

MASM console window creation troubles (maybe my stack frame??)


winapi,assembly,stack,x86-64,masm
I'm having trouble with the following code snippet. I'm compiling an asm file with a c++ file using visual studio express 2012. I've tried debugging calculating the addresses of the stack pointers and memory locations (still learning assembly obviously), but can't find a fault. (As I'm new it's probably something...

assembly function with C segfault


c,assembly,x86,sse,fpu
I am trying to make assembly function that uses SSE and FPU for parallel calculations. Unfortunately I am receiving segmentation fault(core dumped) error(while debugging it doesn't show in assembly function). I also cannot step out from assembly function. Gdb shows: Warning: Cannot insert breakpoint 0. Cannot access memory at address...

Make the input wait for mouse or keyboard - Assembly Language


assembly,input,keyboard,mouse,simultaneous
I fixed my program, but now the loop seems to be stuck. Whenever I press u or d, it is stuck, but the mouse part works :) ; You may customize this and other start-up templates; ; The location of this template is c:\emu8086\inc\0_com_template.txt org 100h CLEAR MACRO MOV AX,0600H...

How to find illegal instructions in a program?


c++,c,assembly,gdb,benchmarking
I have a benchmark which is meant to run on a specific simulator, Some instructions where added to the benchmark to communicate with the simulator(not perform CPU operations), like dump stats or reset stats and so on. Now I need to run the same benchmarks on another simulator, and there...

How to represent mips instruction as it's hex representation


assembly,mips
I'm given a MIPS instruction: top: lw $t1, ($t0) beq $s0, $0, end jal func add $s0, $s0, $t0 addi $s0, $s0, -1 j top bne $s0, $0, top end: func: sll $v0, $t1, 4 jr $ra and am told to convert each line to the "instruction in hex." What...

Declaring Variables in the .data Versus on the Stack - ASM


assembly
I'm trying to understand in which cases you would want to declare variables in the .data section of my assembly program and using it for the procedures needed, over instantiating local variables on the stack and vice versa. Is it just that declaring variables in the .data section is only...

Understanding NASM Macro


assembly,macros,ffmpeg,nasm
I've come around this macro in a assembly source file and I just can't figure out how it's working. So first I come around this function (hevc_deblock.h): cglobal hevc_v_loop_filter_chroma_8, 3, 5, 7, pix, stride, tc, pix0, r3stride sub pixq, 2 lea r3strideq, [3*strideq] mov pix0q, pixq add pixq, r3strideq TRANSPOSE4x8B_LOAD...

Print string using INT 0x10 in bootsector


assembly,x86,fasm
I want to create printl function that allow me to print string in the ax register. I am in 16-bit real mode and I can not find any way to print a message. I using int 0x10 to print a single letter. I try pass argument (string to print) in...

Why Masm32 only give 1 to 100 result for add and subtraction operation and beyond that I got wrong answer?


assembly,masm32
I am new to assembly language. I make this code just now and run it with no errors so far, except It will only give a result from 1 - 100, here's my code. This is a simple math operation which is addition and subtraction. I tried analyzing the code...

MinGW's ld cannot perform PE operations on non PE output file


gcc,assembly,mingw,nasm,osdev
I know there are some other similar questions about this out there, be it StackOverflow or not. I've researched a lot for this, and still didn't find a single solution. I'm doing an operative system as a side project. I've been doing all in Assembly, but now I wanna join...

NASM assembled bootloader memory issue


assembly,nasm,bootloader
I am writing a bootloader with nasm. At the moment it is designed to output a welcome string, then record keystrokes while displaying them, printing the stored keystrokes upon finding the enter key, and finally halting. bits 16 org 0x7C00 start: jmp main bgetkey: pusha mov ax, 0 mov ah,...

How does this instruction look in memory?


assembly,x86
I am having trouble figuring out how this instruction looks in memory for a x86 processor. mov $0x3c,%eax Can someone help me figure it out? For example an easy one is: xor %edi,%edi ---> 0x31 0xFF...

What does DX + 2 mean in mov ah,9 int 21h?


assembly
mov dx, offset buffer mov ah, 0ah int 21h jmp print buffer db 10,?, 10 dup(' ') print: xor bx, bx mov bl, buffer[1] mov buffer[bx+2], '$' mov dx, offset buffer + 2 mov ah, 9 int 21h I know buffer[bx+2] stands for '$', but offset buffer +2 in mov...

MASM SEG operator


assembly,masm
Let's assume we are writing assembly code using MASM 6.1 / 16 bit / huge data model, and we have a variable (label) named MY_VAR, a segment named MY_SEG and a segment group named MY_GROUP. Let's assume MY_VAR is placed in MY_SEG, and MY_SEG belongs to MY_GROUP. Then, what is...

NASM: copying a pointer from a register to a buffer in .data


linux,assembly,nasm,x86-64
I am new to asm. I am trying to copy a pointer from a register to a .data variable using NASM, on linux 64-bit. Concider this program: section .data ptr: dq 0 section .text global _start _start: mov [ptr], rsp mov rax, 60 mov rdi, 0 syscall Here I try...

Perform integer division using multiplication


assembly,optimization,bit-manipulation,division,multiplication
Looking at x86 assembly produced by a compiler, I noticed that (unsigned) integer divisions are sometimes implemented as integer multiplications. These optimizations seem to follow the form value / n => (value * ((0xFFFFFFFF / n) + 1)) / 0x100000000 For example, performing a division by 9: 12345678 / 9...

Error: Junk at EOL, first unrecognised character is '('


assembly,gas,quake
I am trying to compile Quake 1 (QW) from source code and have run into the following problem when compiling client/d_copy.s : Error: Junk at EOL, first unrecognised character is '('. The file in question is here (please excuse the syntax highlighting, pastebin only supports NASM). I am attempting to...

How is shellcode generated from C? - With code example


python,c,gcc,assembly,shellcode
I started taking an online course regarding software security. In one of the sections, I was instructed to execute a hidden C function using a buffer overflow. I got to thinking: what would happen if I could pass machine instruction directly to a stack unsafe executable? What I have been...

Macro to push arguments onto stack


visual-c++,assembly,macros
I've been working on a fun little project to mess around with how functions are called and I need a macro to push arguments as it'll be quite time consuming to push the arguments manually for every instance of this obfuscated call. This is my code so far: #define pushargs(...)...

LC3 assembly-unable to print the right character


assembly,encryption,lc3
I have been trying to make a program that decrypts a text that is being read from a file under the following condiditons: The adress of the first element of the file is 5001.Each letter is encrypted twice:The first time with Ceasar encryption and the second time with XOR encryption.The...

x86 jmp asterisk %eax


assembly,x86,jmp
In the paper for Google Native Client, the authors define a nacljmp as these two instructions: and %eax, 0xffffffe0 // Clears the 5 least significant bits in %eax. jmp *%eax First, I can see they clear the 5 least signficiant bits to make the instruction 32 bit aligned before it...

How is a file loaded in the FAT file system?


filesystems,fat32,assembly
I am developing a bootloader in x86 Assembly and I have a question about the FAT file system. Which steps are necessary to find and load the file?...

subl causing Floating point exception


assembly,x86
I am creating a compiler which should compile a fictional language into asm x86 code. When compiling this piece of code (fictional code): int x; int f(int n) { write n; } int main() { x = 1; f(x); } write is equal to just print in console. The desired...

Asm x86 segmentation fault in reading from file


assembly,x86,segmentation-fault,mmap
I'm receiving segmentation fault in 5th line of loop2. This is the part of my code which is reading values from mmapped file byte by byte. Could you please tell me what I'm doing wrong? .global _start _start: mov $2,%rax mov 16(%rsp),%rdi mov $02,%rsi syscall cmp $0,%rax jl exit 1:...

How to jump to an address saved in a register in intel assembly? [duplicate]


assembly,jmp
This question already has an answer here: conditional jump to register 1 answer say I calculated an address of a label and stored it in eax register, how can I JMP(specifically JE) to eax? jmp eax compiles, but I didn't check if it works. je eax doesn't compile(invalid combination...

Range of immediate values in ARMv8 A64 assembly


gcc,assembly,arm64
My understanding is that immediate parameters in ARMv8 A64 assembly can be 12 bits long. If that is the case, why does this line of assembly code: AND X12, X10, 0xFEF Produce this error (when compiled with gcc) Error: immediate out of range at operand 3 -- `AND X12, X10,...

Print a number in NASM - building an x86 Bootsector


assembly,nasm
I just started messing around with assembly language and I tried to print the number 9 on console. Here is what I wrote: global _main section .data digit equ 9 section .bss section .text _main: mov edx, 1 mov ecx, digit add ecx, 48 mov ebx, 1 mov eax, 4...

How do I check assembly output of Java code?


java,assembly
I found this question that answered it for C++: How do you get assembler output from C/C++ source in gcc?

Range of Addresses for a Conditional Branch Instruction in MIPS


assembly,mips
What is the range of addresses for a conditional branch instruction in MIPS with respect to X, where X contains the address of the branch instruction? Assume the current PC value is 0x10000000. What is the range of addresses for jump (j) and the jump and link (jal) in- structions...