Advertisement
ewelina_r

Untitled

May 25th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #----------------------------------------------------------------
  2. # Program lab_4a.s - Asemblery Laboratorium IS II rok
  3. #----------------------------------------------------------------
  4. #
  5. #  To compile: as -o lab_4a.o lab_4a.s
  6. #  To link:    ld -o lab_4a lab_4a.o
  7. #  To run:     ./lab_4a
  8. #
  9. #----------------------------------------------------------------
  10.  
  11.     .equ    kernel, 0x80    # Linux system functions entry
  12.     .equ    create, 0x08    # create file function
  13.     .equ    close,  0x06    # close file function
  14.     .equ    write,  0x04    # write data to file function
  15.     .equ    exit,   0x01    # exit program function
  16.  
  17.     .equ    mode,   0x180   # attributes for file creating
  18.  
  19.     .equ    stderr, 2
  20.     .equ    errval, 2
  21.  
  22.     .data
  23.    
  24. file_n:             # file name (0 terminated)
  25.     .string "testfile.txt"
  26.  
  27. file_h:             # file handle
  28.     .long       0
  29.  
  30. txtline:            # text to be written to file
  31.     .ascii  "A line of text\n"
  32.  
  33. txtlen:             # size of written data
  34.     .long       ( . - txtline )
  35.  
  36. errmsg:             # file error message
  37.     .ascii  "File error!\n"
  38.  
  39. errlen:
  40.     .long       ( . - errmsg )
  41.  
  42. allokmsg:           # All OK message
  43.     .ascii  "\nAll is OK - too hard to believe!\n"
  44.  
  45. alloklen:
  46.     .long       ( . - allokmsg )
  47.  
  48.     .text
  49.     .global _start
  50.    
  51. _start:
  52.     NOP
  53.     MOVL    $create,%eax    # create function
  54.     MOVL    $file_n,%ebx    # EBX points to file name
  55.     MOVL    $mode,%ecx  # mode of created file in ECX
  56.     INT $kernel
  57.    
  58.     CMP $0,%eax
  59.     JL  error       # if EAX<0 then something went wrong
  60.  
  61.     MOVL    %eax,file_h # store file handle returned in EAX
  62.  
  63.     MOVL    $write,%eax # write function
  64.     MOVL    file_h,%ebx # file handle in EBX
  65.     MOVL    $txtline,%ecx   # ECX points to data buffer
  66.     MOVL    txtlen,%edx # bytes to be written
  67.     INT $kernel
  68.  
  69.     CMP %edx,%eax
  70.     JNZ error       # if EAX<>EDX then something went wrong
  71.  
  72.     MOVL    $close,%eax # close function
  73.     MOVL    file_h,%ebx # file handle in EBX
  74.     INT $kernel
  75.  
  76.     CMP $0,%eax
  77.     JL  error       # if EAX<0 then something went wrong
  78.  
  79. all_ok:
  80.     MOVL    $write,%eax # write function
  81.     MOVL    $stderr,%ebx    # file handle in EBX
  82.     MOVL    $allokmsg,%ecx  # ECX points to All OK message
  83.     MOVL    alloklen,%edx   # bytes to be written
  84.     INT $kernel
  85.  
  86.     XOR %ebx,%ebx
  87.     JMP theend
  88.  
  89. error:
  90.     MOVL    $write,%eax # write function
  91.     MOVL    $stderr,%ebx    # file handle in EBX
  92.     MOVL    $errmsg,%ecx    # ECX points to file error message
  93.     MOVL    errlen,%edx # bytes to be written
  94.     INT $kernel
  95.  
  96.     MOVL    $errval,%ebx
  97.  
  98. theend:
  99.     MOVL    $exit,%eax  # exit program function
  100.     INT $kernel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement