CozyCoder

Save single byte to file

Aug 24th, 2017
408
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.     call closeFileIn        ;close file
  9.  
  10.     call openFileOut        ;open output file
  11.     call writeToFile        ;write the data in data
  12.     call closeFileOut       ;close file
  13.  
  14.     call exitProg           ;exit process
  15.  
  16. openFileIn:
  17.     ;opening the file roller1.raw
  18.     mov eax, 5
  19.     mov ebx, fileName
  20.     mov edx, 0777
  21.     int 0x80
  22.  
  23.     mov [fd_in], eax    ;get pointer to indexbuffer
  24.     ret
  25.  
  26. openFileOut:
  27.     ;opening the file roller1.raw
  28.     mov eax, 5
  29.     mov ebx, outName
  30.     ;mov ecx, 1
  31.     mov edx, 0777
  32.     int 0x80
  33.  
  34.     mov [fd_out], eax   ;get pointer to indexbuffer
  35.     ret
  36.  
  37. writeToFile:
  38.     ;1. Put sys_write(), 4, in EAX
  39.     ;2. Put fileDesc in EBX
  40.     ;3. Put the pointer to data in ECX
  41.     ;4. Put buffer size of data in EDX
  42.     ;5. call kernel
  43.  
  44.     ;returns the actual nr of bytes written in EAX
  45.  
  46.     mov eax, 4
  47.     mov ebx, [fd_out]
  48.     mov ecx, info + 2      ;pointer to array + 2 bytes
  49.     mov edx, 1             ;Nr of bytes to be printed
  50.     int 0x80
  51.  
  52.     ret
  53.  
  54. readFromFileIn:
  55.     ;Process: Read from file
  56.     ;1. put the system call sys_read() number 3 in EAX
  57.     ;2. Put the file descriptor in the EBX register
  58.     ;3. Put the pointer to the input buffer in the ECX register
  59.     ;4. Put the buffer size, in EDX
  60.     ;5. call kernel
  61.  
  62.  
  63.     ;the system call returns the number of bytes rad in the EAX
  64.     ;register, in case of error code is in the EAX register
  65.  
  66.     ;read the file
  67.     mov eax, 3
  68.     mov ebx, [fd_in]    ;pointer to inputBuffer
  69.     mov ecx, info       ;buffer size
  70.     mov edx, IMAGE_SIZE
  71.     int 0x80
  72.  
  73.     ret
  74.  
  75. readFromFileOut:
  76.     ;Process: Read from file
  77.     ;1. put the system call sys_read() number 3 in EAX
  78.     ;2. Put the file descriptor in the EBX register
  79.     ;3. Put the pointer to the input buffer in the ECX register
  80.     ;4. Put the buffer size, in EDX
  81.     ;5. call kernel
  82.  
  83.  
  84.     ;the system call returns the number of bytes rad in the EAX
  85.     ;register, in case of error code is in the EAX register
  86.  
  87.     ;read the file
  88.     mov eax, 3
  89.     mov ebx, [fd_in]    ;pointer to inputBuffer
  90.     mov ecx, info       ;buffer size
  91.     mov edx, IMAGE_SIZE
  92.     int 0x80
  93.  
  94.     ret
  95.  
  96. printBuffer:
  97.     ;1. put syscall sys_write to EAX
  98.     ;2. put an argument in EBX
  99.     ;3. put the data into ECX
  100.     ;4. put the size of the segment in EDX
  101.     ;5. call kernel
  102.     mov eax, 4
  103.     mov ebx, 1
  104.     mov ecx, info
  105.     mov edx, IMAGE_SIZE
  106.    
  107.     int 0x80
  108.     ret
  109.  
  110. closeFileIn:
  111.     ;Closing a file
  112.     ;1.Put the system call sys_close(), 6, in the EAX register
  113.     ;2.Put the file descriptor in the EBX register
  114.     mov EAX, 6
  115.     mov EBX, [fd_in]
  116.     int 80h
  117.  
  118.     ;the system call returns error code in eax
  119.     ret
  120.  
  121. closeFileOut:
  122.     ;Closing a file
  123.     ;1.Put the system call sys_close(), 6, in the EAX register
  124.     ;2.Put the file descriptor in the EBX register
  125.     mov EAX, 6
  126.     mov EBX, [fd_out]
  127.     int 80h
  128.  
  129.     ret
  130.  
  131. exitProg:
  132.     ;call system exit
  133.     mov eax, 1
  134.     int 0x80
  135.  
  136. section .data
  137. fileName db 'roller1.raw', 0 ;glöm förfan inte nollan
  138. ;lenfFileName equ $-fileName ; adress
  139. outName db 'roller2.raw', 0
  140. ;lenOutName equ $-fileName
  141. IMAGE_SIZE equ 64256
  142.  
  143. section .bss
  144. fd_out resb 1
  145. fd_in resb 1
  146. info resb 64256
Advertisement
Add Comment
Please, Sign In to add comment