0

So, I have the following code:

section .data
   sizebuf dw      1024
section .bss
   buf     resb    1024

section .text

global _start
_start:

  ; put bmp file name in rbx
    pop rbx
    pop rbx
    pop rbx

  ; open BMP
    mov eax,  5
    mov ebx, rbx
    mov rcx,  0
    int 80h

  ; read BMP
    mov eax,  3
    mov ebx,  eax
    mov ecx,  buf
    mov edx,  sizebuf
    int 80h

Basically I want this code to open a bmp image and read bytes 3-6 and 11-14, storing them somewhere in order to know the file size and offset. Right now I think it just opens and reads the whole file and I can't seem to figure out how to read those specific bytes, even after reading some similar questions (sorry if duplicate). I am a complete beginner in assembly so any help is appreciated. Thanks!

  • 1
    Look up the `pread` system call which allows you to specify the position to read from, as well as the number of bytes to read. Or just read all 14 bytes and ignore the ones you don't care about. – Nate Eldredge Dec 21 '20 at 19:03
  • I might be completely wrong, but from what i have seen pread seems to be a linux instruction, how can I use it in this program? thanks for the help! – João Matos Dec 22 '20 at 17:19
  • Yes, it's a Linux system call, but the rest of your code seems to be making Linux system calls already (that's what `int 80h` is). Are you not intending to write a program for Linux? – Nate Eldredge Dec 22 '20 at 18:23
  • Yes yes, I want it to work in Linux, what I meant to say is that sasm does not seem to recognize the system call, what is the exact syntax I should use in this case? I cant really find much information about it and perhaps I am using it incorrectly. – João Matos Dec 22 '20 at 19:05
  • 1
    It's a system call just like the ones you are making in your current code. `read()` is system call number 3, so you invoke `int 0x80` with `eax = 3`. `pread()` is system call number `0xb4`, see https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md#x86-32_bit, so you set `eax = 0xb4` instead. There are some slightly tricky issues with passing a 64-bit offset into the file, so maybe it's easier for a beginner to just read all 14 bytes and pick out what you care about. – Nate Eldredge Dec 22 '20 at 19:25
  • Thank you so much and I am sorry to keep bothering you, but how can I ignore the bytes I dont need? I have never done anyting like that before – João Matos Dec 22 '20 at 22:17

0 Answers0