CozyCoder

Access each byte in raw file

Aug 24th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. section .text
  2.     global _start
  3.  
  4. _start:
  5.  
  6.     call openFileIn         ;open the org image
  7.     call readFromFileIn     ;read the data
  8.  
  9.     ;call printBuffer
  10.  
  11.     ;call openFileOut       ;open output file
  12.     ;call writeToFile       ;write the data in data
  13.     ;call closeFileOut      ;close file
  14.     ;call closeFileIn       ;close file
  15.  
  16.     MOV ecx, fd_in          ;store the pointer to the data     
  17.     INC ecx                 ;increase the counter, i++
  18.  
  19.     MOV ebx, [ecx]          ;store the value of that adress
  20.  
  21.     CMP [testWord], ebx     ;I expect ebx to hold 0x00 (one byte size)
  22.     JE printYay             ;If they are equal, print yay to shell
  23.  
  24.     MOV ecx, ebx            ;store value in ebx in a desperate attempt for
  25.                             ;getting some insight of the value i've got
  26.  
  27.    
  28.  
  29.     ;print out first element in info
  30.     ;1. put syscall sys_write to EAX
  31.     ;2. put an argument in EBX
  32.     ;3. put the data into ECX
  33.     ;4. put the size of the segment in EDX
  34.     ;5. call kernel
  35.     MOV eax, 4
  36.     MOV ebx, 1
  37.     ;MOV ecx, char
  38.     MOV edx, 1
  39.     int 0x80
  40.  
  41.     ;The value does not give me any character or symbol in bash when program
  42.     ;is run
  43.  
  44.     call exitProg           ;exit process
  45.  
  46. openFileIn:
  47.     ;opening the file roller1.raw
  48.     MOV eax, 5
  49.     MOV ebx, fileName
  50.     MOV edx, 0777
  51.     int 0x80
  52.  
  53.     MOV [fd_in], eax    ;get pointer to indexbuffer
  54.     ret
  55.  
  56. openFileOut:
  57.     ;opening the file roller1.raw
  58.     MOV eax, 5
  59.     MOV ebx, outName
  60.     MOV edx, 0777
  61.     int 0x80
  62.  
  63.     MOV [fd_out], eax   ;get pointer to indexbuffer
  64.     ret
  65.  
  66. writeToFile:
  67.     ;1. Put sys_write(), 4, in EAX
  68.     ;2. Put fileDesc in EBX
  69.     ;3. Put the pointer to data in ECX
  70.     ;4. Put buffer size of data in EDX
  71.     ;5. call kernel
  72.  
  73.     ;returns the actual nr of bytes written in EAX
  74.  
  75.     MOV eax, 4
  76.     MOV ebx, [fd_out]
  77.     MOV ecx, info
  78.     MOV edx, IMAGE_SIZE
  79.     int 0x80
  80.     ret
  81.  
  82. readFromFileIn:
  83.     ;Process: Read from file
  84.     ;1. put the system call sys_read() number 3 in EAX
  85.     ;2. Put the file descriptor in the EBX register
  86.     ;3. Put the pointer to the input buffer in the ECX register
  87.     ;4. Put the buffer size, in EDX
  88.     ;5. call kernel
  89.  
  90.  
  91.     ;the system call returns the number of bytes rad in the EAX
  92.     ;register, in case of error code is in the EAX register
  93.  
  94.     ;read the file
  95.     MOV eax, 3
  96.     MOV ebx, [fd_in]    ;pointer to inputBuffer
  97.     MOV ecx, info       ;buffer size
  98.     MOV edx, IMAGE_SIZE
  99.     int 0x80
  100.  
  101.     ret
  102.  
  103. readFromFileOut:
  104.     ;Process: Read from file
  105.     ;1. put the system call sys_read() number 3 in EAX
  106.     ;2. Put the file descriptor in the EBX register
  107.     ;3. Put the pointer to the input buffer in the ECX register
  108.     ;4. Put the buffer size, in EDX
  109.     ;5. call kernel
  110.  
  111.  
  112.     ;the system call returns the number of bytes rad in the EAX
  113.     ;register, in case of error code is in the EAX register
  114.  
  115.     ;read the file
  116.     MOV eax, 3
  117.     MOV ebx, [fd_in]    ;pointer to inputBuffer
  118.     MOV ecx, info       ;buffer size
  119.     MOV edx, IMAGE_SIZE
  120.     int 0x80
  121.  
  122.     ret
  123.  
  124. printBuffer:
  125.     ;1. put syscall sys_write to EAX
  126.     ;2. put an argument in EBX
  127.     ;3. put the data into ECX
  128.     ;4. put the size of the segment in EDX
  129.     ;5. call kernel
  130.     MOV eax, 4
  131.     MOV ebx, 1
  132.     MOV ecx, info
  133.     MOV edx, IMAGE_SIZE
  134.  
  135.     int 0x80
  136.     ret
  137.  
  138. closeFileIn:
  139.     ;Closing a file
  140.     ;1.Put the system call sys_close(), 6, in the EAX register
  141.     ;2.Put the file descriptor in the EBX register
  142.     MOV EAX, 6
  143.     MOV EBX, [fd_in]
  144.     int 80h
  145.  
  146.     ;the system call returns error code in eax
  147.     ret
  148.  
  149. closeFileOut:
  150.     ;Closing a file
  151.     ;1.Put the system call sys_close(), 6, in the EAX register
  152.     ;2.Put the file descriptor in the EBX register
  153.     MOV EAX, 6
  154.     MOV EBX, [fd_out]
  155.     int 80h
  156.  
  157.     ret
  158.  
  159. conditionalTest:
  160.     INC edx
  161.     CMP edx, 10         ;Compare dx with 0
  162.     JLE conditionalTest
  163.  
  164.     call printYay
  165.  
  166.     ret
  167.  
  168. printYay:
  169.     ;output the message
  170.     MOV eax, 4
  171.     MOV ebx, 1
  172.     MOV ecx, yayString
  173.     MOV edx, yayStringLen
  174.     int 0x80
  175.    
  176.     call exitProg
  177.  
  178. exitProg:
  179.     ;call system exit
  180.     mov eax, 1
  181.     int 0x80
  182.  
  183.  
  184. section .bss
  185. fd_out resb 1
  186. fd_in resb 1
  187. info resb 64256
  188.  
  189. char resb 1
  190.  
  191.  
  192. section .data
  193.  
  194. fileName db 'roller2.raw', 0 ;glöm förfan inte nollan
  195. outName db 'roller3.raw', 0
  196. IMAGE_SIZE equ 64256
  197.  
  198. yayString db 'Yay', 0
  199. yayStringLen equ $ - yayString
  200.  
  201. testWord DW 0x00
Advertisement
Add Comment
Please, Sign In to add comment