Guest User

Untitled

a guest
Aug 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. assembler- Pass parameter and use it in program
  2. movl 16(%ebp),%eax
  3.  
  4. .section .data
  5. #######PROGRAM CODE###
  6.  
  7. .section .text
  8.  
  9. #STACK POSITIONS
  10. .equ ST_SIZE_RESERVE, 8
  11. .equ ST_FD_IN, 0
  12. .equ ST_FD_OUT, 4
  13. .equ ST_ARGC, 8 #Number of arguments
  14. .equ ST_ARGV_0, 12 #Name of program
  15. .equ ST_ARGV_1, 16 #Input file name
  16. .equ ST_ARGV_2, 20 #Output file name
  17. .equ ST_ARGV_3, 24 #-a or -l or nothing
  18. .equ ST_EXIT_CODE, 28 #Exit code
  19.  
  20. .globl _start
  21. _start:
  22. ###INITIALIZE PROGRAM###
  23. subl $ST_SIZE_RESERVE, %esp #Allocate space for our pointers on the stack
  24. movl %esp, %ebp
  25.  
  26. ### Set standard error code 0
  27. movl $0, %ebx
  28. movl %ebx, ST_EXIT_CODE(%ebp)
  29. movl ST_ARGV_3(%ebp),%eax #eax contains now the alternative paramter
  30.  
  31. if (strcmp(argv[3], "-a") == 0) {
  32. /* stuff here */
  33. }
  34.  
  35. int strcmp(const char* str1, const char* str2) {
  36. while (*str1 && *str1 == *str2) {
  37. str1++;
  38. str2++;
  39. }
  40. return *str1 - *str2;
  41. }
  42.  
  43. strcmp:
  44. push %ebp
  45. mov %esp, %ebp
  46. movl 0x8(%ebp), %esi # str1
  47. movl 0xc(%ebp), %edi # str2
  48. 1:
  49. movb (%esi), %al
  50. or %al, %al # *str1 zero?
  51. jz 2f
  52. movb (%edi), %ah
  53. cmp %ah, %al # equal to *str2?
  54. jne 2f
  55. # move to next character
  56. inc %esi
  57. inc %edi
  58. jmp 1b
  59. 2:
  60. # result is difference between the two characters compared
  61. movb (%esi), %al
  62. subb (%edi), %al
  63. movsx %al, %eax # Sign-extend to 32-bits
  64. pop %ebp
  65. ret
  66.  
  67. # [snip]
  68. movl ST_ARGV_3(%ebp),%eax #eax contains now the alternative paramter
  69.  
  70. push %eax # first argument
  71. pushl $dasha # second argument "-a"
  72. call strcmp # compare strings
  73. add $8, %esp # pop arguments
  74.  
  75. cmpl $0, %eax # Strings equal?
  76. je strings_are_equal # Subtitute your own logic here
  77. jmp strings_are_not_equal # and here..
  78.  
  79. .section .data
  80. dasha: .asciz "-a"
Add Comment
Please, Sign In to add comment