Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. .section .data
  2.  
  3. # System call numbers
  4. .equ OPEN, 2
  5. .equ WRITE, 1
  6. .equ READ, 0
  7. .equ CLOSE, 3
  8. .equ EXIT, 60
  9.  
  10. # File modes
  11. .equ O_RDONLY, 0
  12. .equ O_CREAT_WRONLY_TRUNC, 03101
  13. .equ O_PERMS, 0666
  14.  
  15. .equ END_OF_FILE, 0 # End of file
  16.  
  17.  
  18. # variables
  19. IMAGE_SIZE: .long 0 # bitmap data size
  20. IMAGE_WIDTH: .long 0 # bitmap width
  21. IMAGE_HEIGHT: .long 0 # bitmap height
  22. B_CHAR: .byte 66 # int value of letter B
  23. M_CHAR: .byte 77 # int value of letter M
  24. BITS_PER_PIXEL .word 24 # value of bits per pixel for our image
  25. COLOR_PALETTE .long 0 # color palette value for this assignment
  26.  
  27. message_bytes: .quad 0 # the amount of bytes for the message
  28.  
  29. .section .bss
  30. .equ BUFFER_SIZE, 256
  31. .lcomm WRITE_DATA, BUFFER_SIZE
  32. .equ HEADER_SIZE, 54
  33. .lcomm HEADER_DATA, HEADER_SIZE
  34.  
  35.  
  36. .section .text
  37.  
  38. # RETURN VALUES
  39. .equ RETURN_SUCCESS, 0
  40. .equ RETURN_INVALID_NUM_ARGS, 1
  41. .equ RETURN_ERR_INPUT, 2
  42. .equ RETURN_ERR_OUTPUT, 3
  43. .equ RETURN_INVALID_FORMAT, 4
  44. .equ RETURN_MSG_TOO_LONG, 5
  45.  
  46. # BMP HEADER OFFSETS
  47. .equ HO_B, 0
  48. .equ HO_M, 1
  49. .equ HO_SIZE_V3, 14
  50. .equ HO_WIDTH_PIXELS, 18
  51. .equ HO_HEIGHT_PIXELS, 22
  52. .equ HO_NUM_BITS_PIXEL, 28
  53. .equ HO_COMPRESSION, 30
  54. .equ HO_NUM_COLORS_PALETTE, 46
  55.  
  56. .globl _start
  57. _start:
  58. ###INITIALIZE PROGRAM###
  59. # r12 = file descriptor (INPUT)
  60. # r13 = file descriptor (OUTPUT)
  61. popq %rdi # load number of arguments into rdi
  62. popq %rcx
  63. popq %rcx # Argument 0 - rcx = hidden message
  64. popq %r8 # Argument 1 - r8 = filepath(input)
  65. popq %r9 # Argument 2 - r9 = filepath(output)
  66.  
  67. ### check for valid amount of arguments ###
  68. cmpq $4, %rdi
  69. jne error_arguments # jump to errorhandling
  70.  
  71. ### check if the bmp-input file exists ###
  72. movq %r8, %rdi # move filepath(input) into %rdi
  73. movq $OPEN, %rax # move open file number into %rax
  74. movq $O_RDONLY, %rsi # move read only number into %rsi
  75. syscall # call linux to open the file :)
  76. cmpq $0, %rax # check if file opening was successfull (return == 0)
  77. jl error_open_file # jump to errorhandling
  78. movq %rax, %r12 # move file descriptor(input) from %rax to r12
  79.  
  80. ### check the output file ###
  81. movq %r9, %rdi # move filepath(output) into %rdi
  82. movq $OPEN, %rax # move open file number into %rax
  83. movq $O_PERMS, %rdx # move permissions number into %rdx
  84. movq $O_CREAT_WRONLY_TRUNC, %rsi # move write only number into %rsi
  85. syscall # create the file
  86. cmpq $0, %rax # check if file creation was successful (return == 0)
  87. jl error_output_file
  88. movq %rax, %r13 # move file descriptor(output) from %rax to r13
  89.  
  90. ### read the header information ####
  91. movq %r12, %rdi # move file descriptor(input) into %rdi
  92. movq $READ, %rax # move read file number into %rax
  93. movq $HEADER_DATA, %rsi # move starting address into %rsi
  94. movq $HEADER_SIZE, %rdx # move buffer length into %rdx
  95. syscall # do it (read the file)
  96. cmpq $0, %rax # check if file opening was successful
  97. jl error_read_file
  98. cmpq $HEADER_SIZE, %rax # check if header size is valid
  99. jl error_format
  100.  
  101. ### check the header information ###
  102. movq $HEADER_DATA, %rbx
  103. movq $HO_B, %rdi # move offset for HO_B information into %rdi
  104. movb (%rbx, %rdi, 1), %ah # move the byte at offset into %ah
  105. movq $HO_M, %rdi # move offset for HO_M information into %rdi
  106. movb (%rbx, %rdi, 1), %al # move the bate at offset into %al
  107. cmpb B_CHAR, %ah # check if B is at the given position in the header
  108. jne error_format
  109. cmpb M_CHAR, %al # check if M is at the given position in the header
  110. jne error_format
  111.  
  112. ### check the color depth information ###
  113. movq $HO_NUM_BITS_PIXEL, %rdi # move offset for $HO_NUM_BITS_PIXEL information into %rdi
  114. movb (%rbx, %rdi, 1), %al # move the byte at offset into %al
  115. inc %rbi # increment %rbi to get the the second part of the value
  116. movb (%rbx, %rdi, 1), %ah # move the byte at offset into %ah
  117. cmpw BITS_PER_PIXEL, %ax # check if color depth is valid in our file
  118. jne error_format
  119.  
  120. ### check the compression method information ###
  121. movq $0, %rdi # the compression imformation has to be 0, so move 0 into %rdi
  122. movq $HO_COMPRESSION, %r14 # move the compression offset into %r14
  123. movl HEADER_DATA(%r10, %rdi, 4), %eax # move the compression information into %eax
  124. cmpl $0, %eax # check if the comperssion method is 0
  125. jne error_format
  126.  
  127. ### check the color palette information ####
  128. movq $0, %rdi # move 0 into %rdi
  129. movq $HO_NUM_COLORS_PALETTE, %r14 # move the collor palette offset innto %r14
  130. movl HEADER_DATA(%r14, %rdi, 4), %eax # move the data into %eax
  131. cmpl $COLOR_PALETTE, %eax # the color palette information has to be 0
  132. jne error_format
  133.  
  134. ### check the size of the image ###
  135. ## get width and height ##
  136. movq $0,%rdi # move 0 into %rdi
  137. movq $HO_WIDTH_PIXELS, %r14 # move offset for pixel width into %r14
  138. movl HEADER_DATA(%r14, %rdi, 4), IMAGE_WIDTH # move the value into variable width
  139. movq $HO_HEIGHT_PIXELS, %r14 # move offset for picel height into %r14
  140. movl HEADER_DATA(%r14, %rdi, 4), IMAGE_HEIGHT # move the value into the variable height
  141. ## actualy check the size ##
  142. movq $0, %rdi # move 0 into %rdi
  143. movq $HO_IMAGE_SIZE, %r14 # move offset for image size into %r14
  144. movl HEADER_DATA(%r14, %rdi, 4), IMAGE_SIZE # move the size information into the variable IMAGE_SIZE
  145. cmpl $0, IMAGE_HEIGHT # check if the height is negative
  146. jl negate_height
  147. jmp get_size
  148.  
  149. negate_height:
  150. movl IMAGE_HEIGHT, %esi # move the image height into %esi
  151. neg %edi # negate the height value in %edi
  152. movl %edi, IMAGE_HEIGHT # move the postive height into the variable IMAGE_HEIGHT
  153.  
  154. get_size:
  155. ### calculate the size of the image ###
  156. # esi = width
  157. # edi = height
  158. movl IMAGE_WIDTH, %esi # move image width into %esi
  159. movl IMAGE_HEIGHT, %edi # move image height into %edi
  160. imul %esi, %edi # multiply them to get the size of the image (pixels)
  161. imul $3, %edi # multiply the size by 3 to get the total bytes of the picture (1 pixel = 3 Byte (RGB))
  162.  
  163. movq %r8, %rcx #
  164. call count_bytes #
  165. #
  166. add $1, %rax #
  167. imul $8, %rax #
  168. cmpl %esi, %eax #
  169. jle encoded_message
  170. jmp error_message_to_long
  171.  
  172. encode_message:
  173. ### todo ... ###
  174.  
  175.  
  176.  
  177. #####ERROR LOOPS#####
  178. # wrong amount of argumments #
  179. error_arguments:
  180. movq $RETURN_INVALID_NUM_ARGS, %rdi # load return error message
  181. jmp exit
  182.  
  183. # something went wrong while opening input file #
  184. error_open_file:
  185. movq %rax, %r10 # preload file descriptor
  186. call close_file # call close file function
  187. movq $RETURN_ERR_INPUT, %rdi # load return error message
  188. jmp exit
  189.  
  190. # somthing went wrong while creating output file #
  191. error_output_file:
  192. movq %rax, %r10 # preload file descriptor
  193. call close_file # call close file function
  194. movq $RETURN_ERR_OUTPUT, %rdi # load return error message
  195. jmp exit
  196.  
  197. # somthing went wrong with reading the file #
  198. error_read_file:
  199. movq $RETURN_READ_ERROR, %rdi # load return message
  200. jmp exit
  201.  
  202. # somthing went wrong with the format while reading file #
  203. error_format:
  204. movq %rax, %r10 # preload file descriptor
  205. call close_file # call close file function
  206. movq $RETURN_INVALID_FORMAT, %rdi # load return message
  207. jmp exit
  208.  
  209. # message is too long for the image #
  210. error_message_to_long:
  211. movq %rax, %r10 # preload file descriptor
  212. call close_file # call close file function
  213. movq $RETURN_MSG_TOO_LONG,%rdi # load return error message
  214. jmp exit
  215.  
  216.  
  217. ###EXIT###
  218. exit:
  219. movq $0, %rbx
  220. movq $EXIT, %rax
  221. syscall
  222.  
  223. ## close file function ##
  224. # closes the file from file descriptor in %r10 #
  225. .type close_file, @function
  226. close_file:
  227. ## save the registers ##
  228. pushq %rbp
  229. movq %rsp, %rbp
  230. pushq %rdi
  231. ## close the file ##
  232. movq $CLOSE, %rax # move close file number into %rax
  233. movq %r10, %rdi # move dile descriptor into rdi
  234. syscall # close file
  235. ## restore the registers ##
  236. popq %rdi
  237. movq %rbp, %rsp
  238. popq %rbp
  239. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement