Advertisement
GoodiesHQ

Read file.txt

Dec 21st, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %define O_RDONLY 0
  2. %define STDOUT 1
  3.  
  4. struc stats
  5.     .dev        resd 1
  6.     .ino        resd 1
  7.     .mode       resw 1
  8.     .nlink      resw 1
  9.     .uid        resw 1
  10.     .gid        resw 1
  11.     .rdev       resd 1
  12.     .size       resd 1
  13.     .blksize    resd 1
  14.     .blocks     resd 1
  15.     .atime      resd 1
  16.     .atime_nsec resd 1
  17.     .mtime      resd 1
  18.     .mtime_nsec resd 1
  19.     .ctime      resd 1
  20.     .ctime_nsec resd 1
  21.     .unused4    resd 1
  22.     .unused5    resd 1
  23. endstruc
  24.  
  25. section .data
  26. fname:      db      "file.txt",0x00
  27. fname_len:  equ     $-fname
  28. msg:        db      "Contents:",0x0a,0x00
  29. msg_len:    equ     $-msg
  30.  
  31. section .bss
  32. stat:       resb    stats_size  ;// adding _size is essentially the sizeof() 'equivalent'
  33. orig_brk:   resd    1           ;// original brk value
  34. tmp:        resd    1           ;// temp buffer if I need it
  35.  
  36. section .text
  37. global _start
  38. _start:
  39.     xor eax, eax
  40.     mov al, 106         ;// Syscall for STAT
  41.     mov ebx, fname
  42.     mov ecx, stat
  43.     int 80h
  44.     ;// the stat struct is now populated. stat.size contains the file size...
  45.  
  46.     xor eax, eax
  47.     xor ebx, ebx
  48.     mov al, 45          ;// Syscall for BRK
  49.     int 80h             ;// EAX has BRK value
  50.     mov [orig_brk], eax
  51.  
  52.     mov ebx, eax
  53.     add ebx, [stat + stats.size]    ;// Add the file size to the BRK value and re-BRK
  54.     xor eax, eax
  55.     mov al, 45          ;// Syscall for BRK
  56.     int 80h             ;// Extends BRK by FILE SIZE
  57.  
  58.     xor eax, eax
  59.     mov al, 5           ;// Syscall for OPEN
  60.     mov ebx, fname
  61.     mov cl, O_RDONLY
  62.     int 80h
  63.     push eax            ;// Saves the File Descriptor on the stack
  64.     mov ebx, eax
  65.  
  66.     xor eax, eax
  67.     mov al, 3           ;// Syscall for READ
  68.     mov ecx, [orig_brk]
  69.     ;xor edx, edx
  70.     mov edx, [stat + stats.size]
  71.     int 80h
  72.  
  73.     xor eax, eax
  74.     mov al, 4           ;// Syscall for WRITE
  75.     mov bl, STDOUT
  76.     mov ecx, [orig_brk]
  77.     ;xor edx, edx
  78.     mov edx, [stat + stats.size]
  79.     int 80h
  80.  
  81.     xor eax, eax
  82.     mov al, 6           ;// Syscall for CLOSE
  83.     pop ebx
  84.     int 80h             ;// Closes the FD
  85.  
  86.     xor eax, eax
  87.     mov al, 1           ;// Syscall for EXIT
  88.     xor ebx, ebx
  89.     int 80h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement