Facebook
From Bitty Duck, 3 Years ago, written in Plain Text.
This paste is a reply to Re: Re: Re: Re: Untitled from Crippled Meerkat - view diff
Embed
Download Paste or View Raw
Hits: 73
  1. #----------------------------------------------------------------
  2. # Program lab_6b.s - Architektury Komputerów
  3. #----------------------------------------------------------------
  4. #
  5. #  To compile&link: gcc -no-pie -o lab_6b lab_6b.s
  6. #  To run:     ./lab_6b
  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. jump_tab:
  43.         .quad oper_err, oper_err, oper_err, oper_3,   oper_err, oper_5,   oper_6,   oper_err
  44.         .quad oper_err, oper_err, oper_10,  oper_err, oper_err, oper_err, oper_err, oper_15
  45.  
  46. #----------------------------------------------------------------
  47.  
  48.         .text
  49.         .global main
  50.  
  51. #----------------------------------------------------------------
  52.        
  53. main:
  54.         push %rbp
  55.  
  56.         cmp $4, %edi            # argc is here
  57.         jnz badnum
  58.  
  59.         mov %rsi, %rbp          # %rbp = %rsi = argv
  60.  
  61.         mov 8(%rbp), %rdi       # atoi( argv[1] ) - 1st argument to %rdi;
  62.         call atoi
  63.         cmp $0, %rax            # result of conversion
  64.         jle badnum              # bad number
  65.         mov %eax, var_a         # store numer in var_a
  66.  
  67.         mov 16(%rbp), %rdi      # atoi( argv[2] ) - 1st argument to %rdi;
  68.         call atoi
  69.         cmp $0, %rax            # result of conversion
  70.         jle badnum              # bad number
  71.         mov %eax, var_b         # store numer in var_b
  72.  
  73.         mov 24(%rbp), %rdi      # atoi( argv[3] ) - 1st argument to %rdi;
  74.         call atoi
  75.         cmp $0, %rax            # result of conversion
  76.         jle badnum              # bad number
  77.         mov %eax, var_oper      # store numer in var_oper
  78.  
  79.         mov var_a, %rdx         # %rdx contains var_a
  80.         mov var_b, %rcx         # %rcx contains var_b
  81.  
  82.         cmp $0, %rax            # check for number of operation
  83.         jl oper_err             # < 0, so error
  84.         cmp $15, %eax           # check for number of operation
  85.         jg oper_err             # > 15, so error
  86.  
  87.         jmp *jump_tab(,%rax,8)  # jump to code of operation                    
  88.  
  89. oper_3:
  90.         and %rcx, %rdx          # result in %rdx
  91.         mov $oper_and, %rcx     # name in %rcx
  92.         jmp display             # jump to display code
  93. oper_5:
  94.         or %rcx, %rdx           # result in %rdx
  95.         mov $oper_or, %rcx      # name in %rcx
  96.         jmp display             # jump to display code
  97. oper_6:
  98.         xor %rcx, %rdx          # result in %rdx
  99.         mov $oper_xor, %rcx     # name in %rcx
  100.         jmp display             # jump to display code
  101. oper_10:
  102.         add %rcx, %rdx          # result in %rdx
  103.         mov $oper_add, %rcx     # name in %rcx
  104.         jmp display             # jump to display code
  105. oper_15:
  106.         sub %rcx, %rdx          # result in %rdx
  107.         mov $oper_sub, %rcx     # name in %rcx
  108.         jmp display             # jump to display code
  109.  
  110. oper_err:
  111.         mov $fmt_err, %rdi      # printf( fmt ) - first argument to %rdi
  112.         mov $0, %al             # printf - number of vector registers to %al
  113.         call printf
  114.         jmp theend
  115.  
  116. display:
  117.         mov %rdx, var_res       # store result in var_res
  118.  
  119.         mov var_b, %r9          # printf( fmt, n1, n1, name, n2, n2 ) - 6th argument to %r9
  120.         mov var_b, %r8          # printf( fmt, n1, n1, name, n2, n2 ) - 5th argument to %r8
  121.         mov var_a, %rdx         # printf( fmt, n1, n1, name, n2, n2 ) - 3rd argument to %rdx
  122.         mov var_a, %rsi         # printf( fmt, n1, n1, name, n2, n2 ) - 2nd argument to %rsi
  123.         mov $fmt_oper, %rdi     # printf( fmt, n1, n1, name, n2, n2 ) - 1st argument to %rdi
  124.         mov $0, %al             # printf - number of vector registers to %al
  125.         call printf
  126.  
  127.         mov var_res, %rdx       # printf( fmt, res, res ) - 3rd argument to %rdx
  128.         mov var_res, %rsi       # printf( fmt, res, res ) - 2nd argument to %rsi
  129.         mov $fmt_res, %rdi      # printf( fmt, res, res ) - 1st argument to %rdi
  130.         mov $0, %al             # printf - number of vector registers to %al
  131.         call printf
  132.  
  133.         jmp theend
  134.  
  135. badnum:
  136.         mov $fmt_bad, %rdi      # printf( fmt ) - first argument to %rdi
  137.         mov $0, %al             # printf - number of vector registers to %al
  138.         call printf
  139.        
  140. theend:
  141.         pop %rbp
  142.  
  143.         ret