1

I am trying to understand the world of Nasm assembly and I've been stuck getting access to a simple element in an Array. I've followed the tutorial at tutorialspoint:

section .text
global _start

_start:

    call openFileIn         ;open the org image
    call readFromFileIn     ;read the data

    ;call printBuffer

    ;call openFileOut       ;open output file
    ;call writeToFile       ;write the data in data
    ;call closeFileOut      ;close file
    ;call closeFileIn       ;close file

    MOV ecx, fd_in          ;store the pointer to the data      
    INC ecx                 ;increase the counter, i++

    MOV ebx, [ecx]          ;store the value of that adress

    CMP [testWord], ebx     ;I expect ebx to hold 0x00 (one byte size)
    JE printYay             ;If they are equal, print yay to shell

    MOV ecx, ebx            ;store value in ebx in a desperate attempt for
                            ;getting some insight of the value i've got



    ;print out first element in info
    ;1. put syscall sys_write to EAX
    ;2. put an argument in EBX
    ;3. put the data into ECX
    ;4. put the size of the segment in EDX
    ;5. call kernel 
    MOV eax, 4
    MOV ebx, 1
    ;MOV ecx, char
    MOV edx, 1
    int 0x80

   ;The value does not give me any character or symbol in bash when program
   ;is run

   call exitProg            ;exit process

I have displayed the code which I believe I do not understand, which is memory accessing in the registers. But if you want to view all the code with syntax highlights, please follow this link: pastebin

I compile with: nasm -f elf <filename>

Link with: ld -m elf_i386 -s -o <outputName> <objectFile>

You can find the files content I get when I read from file, pastebin

8
  • 1
    Multiple problems. First, opening a file does not give you a pointer, it gives you a file descriptor, so incrementing or dereferencing it make no sense. Second, if you want to process characters you should use byte sizes. Third, write expects a pointer to the thing to output. Commented Aug 24, 2017 at 15:55
  • Ouch, ok if I understand you correctly the first problem is that I try to increment a file descriptor, I need to find a way to get to the memory segment in which the array are stored in memory? My hope was that I got a charecter from ecx, which is obviously wrong. So the question is then, how do I get to the data from the file? Commented Aug 24, 2017 at 16:04
  • 1
    ld -m elf_i386 makes a 32-bit binary, not an x32 binary (long mode with 32-bit pointers).. I assume you just mis-tagged it, since you're using the 32-bit int 0x80 ABI as well, not syscall with numbers from /usr/include/asm/unistd_x32.h Commented Aug 24, 2017 at 17:30
  • Thanks Peter, that tag is changed. Commented Aug 24, 2017 at 18:01
  • I already fixed the tags. Your edit changed it back to the wrong tags (so I rolled it back). Commented Aug 24, 2017 at 18:04

1 Answer 1

1

Ok, I solved it, I had to structure things up. If someone has a similar problem to me, please look at the following pastebin, since it may be easier to follow if you are a beginner like me, link.

When I read data from a file, all the data is stored in the my info variable. This array is 64256 bytes long and info holds the pointer to the first element in that segment, I believe in RAM, I am guessing since I doubt that many bytes can fit into a register on the processor?

Anyways, printing a byte, in my case I get the float (1101 0110 in binary, D6 in hexadecimal, and 214 in decimal). Bash do not know how to present this, so in order for me to get the data I need to convert this into ascii, which is more then I can handle for the moment. But this byte is however possible to write to a file. Please look at this code:

mov eax, 4
;thanks Jester, this is not the array indeed
mov ebx, [fd_out]
mov ecx, info + 2    ;info holds the address to the first element
mov edx, 1           ;nr of bytes to be written
int 0x80

info holds the pointer to the first element in the array in memory (RAM, cache, I believe it is some random place in volatile memory). In the ecx register, I store the address of the first memory adress PLUS two bytes, so the address gets an offset of 16 bits in memory, or atleast I think it works like this. :)

Thanks Jester, Peter and duskwuff for the feedback

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.