Facebook
From Crippled Meerkat, 3 Years ago, written in Plain Text.
This paste is a reply to Re: Re: Re: Untitled from Mature Crane - view diff
Embed
Download Paste or View Raw
Hits: 85
  1. #----------------------------------------------------------------
  2. # Program lab_6a.s - Architektury Komputerów
  3. #----------------------------------------------------------------
  4. #
  5. #  To compile&link: gcc -no-pie -o lab_6a lab_6a.s
  6. #  To run:     ./lab_6a
  7. #
  8. #----------------------------------------------------------------
  9.  
  10.         .data
  11.        
  12. oper_and:
  13.         .string "AND"                   # AND operation
  14. oper_or:
  15.         .string "OR"                    # OR operation
  16. oper_xor:
  17.         .string "XOR"                   # XOR operation
  18. oper_add:
  19.         .string "ADD"                   # ADD operation
  20. oper_sub:
  21.         .string "SUB"                   # SUB operation
  22.  
  23. var_a:
  24.         .long   3084                    # first number
  25. var_b:
  26.         .long   1424                    # second number
  27. var_res:
  28.         .long   0                       # result
  29. var_oper:
  30.         .long   0                       # number of operation
  31.  
  32. fmt_oper:
  33.         .string "%d (0x%08x) %s %d (0x%08x) = " # operation format string
  34. fmt_res:
  35.         .string "%d (0x%08x)\n"         # result format string
  36.  
  37. fmt_bad:
  38.         .string "No or bad numbers!\n"  # format for bad arguments
  39. fmt_err:
  40.         .string "Bad operation!\n"      # format for bad operations
  41.  
  42. #----------------------------------------------------------------
  43.  
  44.         .text
  45.         .global main
  46.  
  47. #----------------------------------------------------------------
  48.        
  49. main:
  50.         push %rbp
  51.  
  52.         cmp $4, %edi            # argc is here
  53.         jnz badnum
  54.  
  55.         mov %rsi, %rbp          # %rbp = %rsi = argv
  56.  
  57.         mov 8(%rbp), %rdi       # atoi( argv[1] ) - 1st argument to %rdi;
  58.         call atoi
  59.         cmp $0, %rax            # result of conversion
  60.         jle badnum              # bad number
  61.         mov %eax, var_a         # store numer in var_a
  62.  
  63.         mov 16(%rbp), %rdi      # atoi( argv[2] ) - 1st argument to %rdi;
  64.         call atoi
  65.         cmp $0, %rax            # result of conversion
  66.         jle badnum              # bad number
  67.         mov %eax, var_b         # store numer in var_b
  68.  
  69.         mov 24(%rbp), %rdi      # atoi( argv[3] ) - 1st argument to %rdi;
  70.         call atoi
  71.         cmp $0, %rax            # result of conversion
  72.         jle badnum              # bad number
  73.         mov %eax, var_oper      # store numer in var_oper
  74.  
  75.         mov var_a, %rdx         # %rdx contains var_a
  76.         mov var_b, %rcx         # %rcx contains var_b
  77.  
  78.         cmp $3, %eax            # check for number of first operation
  79.         jnz check_5             # != 3, so check others
  80.         and %rcx, %rdx          # result in %rdx
  81.         mov $oper_and, %rcx     # name in %rcx
  82.         jmp display             # jump to display code
  83. check_5:
  84.         cmp $5, %eax            # check for number of second operation
  85.         jnz check_6             # != 5, so check others
  86.         or %rcx, %rdx           # result in %rdx
  87.         mov $oper_or, %rcx      # name in %rcx
  88.         jmp display             # jump to display code
  89. check_6:
  90.         cmp $6, %eax            # check for number of third operation
  91.         jnz check_10            # != 6, so check others
  92.         xor %rcx, %rdx          # result in %rdx
  93.         mov $oper_xor, %rcx     # name in %rcx
  94.         jmp display             # jump to display code
  95. check_10:
  96.         cmp $10, %eax           # check for number of fourth operation
  97.         jnz check_15            # != 10, so check others
  98.         add %rcx, %rdx          # result in %rdx
  99.         mov $oper_add, %rcx     # name in %rcx
  100.         jmp display             # jump to display code
  101. check_15:
  102.         cmp $15, %eax           # check for number of fifth operation
  103.         jnz oper_err            # != 15, so error
  104.         sub %rcx, %rdx          # result in %rdx
  105.         mov $oper_sub, %rcx     # name in %rcx
  106.         jmp display             # jump to display code
  107.  
  108. oper_err:
  109.         mov $fmt_err, %rdi      # printf( fmt ) - first argument to %rdi
  110.         mov $0, %al             # printf - number of vector registers to %al
  111.         call printf
  112.         jmp theend
  113.  
  114. display:
  115.         mov %rdx, var_res       # store result in var_res
  116.  
  117.         mov var_b, %r9          # printf( fmt, n1, n1, name, n2, n2 ) - 6th argument to %r9
  118.         mov var_b, %r8          # printf( fmt, n1, n1, name, n2, n2 ) - 5th argument to %r8
  119.         mov var_a, %rdx         # printf( fmt, n1, n1, name, n2, n2 ) - 3rd argument to %rdx
  120.         mov var_a, %rsi         # printf( fmt, n1, n1, name, n2, n2 ) - 2nd argument to %rsi
  121.         mov $fmt_oper, %rdi     # printf( fmt, n1, n1, name, n2, n2 ) - 1st argument to %rdi
  122.         mov $0, %al             # printf - number of vector registers to %al
  123.         call printf
  124.  
  125.         mov var_res, %rdx       # printf( fmt, res, res ) - 3rd argument to %rdx
  126.         mov var_res, %rsi       # printf( fmt, res, res ) - 2nd argument to %rsi
  127.         mov $fmt_res, %rdi      # printf( fmt, res, res ) - 1st argument to %rdi
  128.         mov $0, %al             # printf - number of vector registers to %al
  129.         call printf
  130.  
  131.         jmp theend
  132.  
  133. badnum:
  134.         mov $fmt_bad, %rdi      # printf( fmt ) - first argument to %rdi
  135.         mov $0, %al             # printf - number of vector registers to %al
  136.         call printf
  137.        
  138. theend:
  139.         pop %rbp
  140.  
  141.         ret

Replies to Re: Re: Re: Re: Untitled rss

Title Name Language When
Re: Re: Re: Re: Re: Untitled Bitty Duck text 3 Years ago.