Advertisement
austinh115

GCD.asm

Nov 7th, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SECTION .data
  2.  
  3.     msg1:       db "Enter the first number: ", 0
  4.     msg2:       db "Enter the second number: ", 0
  5.     options:    db "Options:", 0xA, \
  6.                    "    1. Command line input",        0xA, \
  7.                    "    2. File input (specify name)", 0xA, \
  8.                                                        0xA, \
  9.                    "Please enter your choice: ", 0
  10.     filechoice: db "Enter your filename: ", 0
  11.     formatin:   db "%d\n", 0
  12.     filenamein: db "%s", 0
  13.     formatout:  db "The GCD of %d and %d is %d.",  0xA, 0
  14.     readmode:   db "r", 0
  15.     errormsg:   db "Reached the end of the file.", 0xA, 0
  16.     num1:       times 4 db 0
  17.     num2:       times 4 db 0
  18.     choice:     times 4 db 0
  19.     filename:   times 100 db 0
  20.     isfilemode: times 4 db 0
  21.  
  22. SECTION .text
  23.  
  24.     global main
  25.     extern scanf
  26.     extern fscanf
  27.     extern printf
  28.     extern fopen
  29.  
  30. main:
  31.  
  32.     push options
  33.     call printf
  34.  
  35.     push choice
  36.     push formatin
  37.     call scanf
  38.  
  39.     cmp dword [choice], 2
  40.     jne cmdline
  41.  
  42. ;
  43.  
  44.     mov dword [isfilemode], 1
  45.  
  46.     push filechoice
  47.     call printf
  48.  
  49.     push filename
  50.     push filenamein
  51.     call scanf
  52.  
  53.     push readmode
  54.     push filename
  55.     call fopen
  56.     add esp, 8
  57.     mov esi, eax
  58.  
  59. fileinput:
  60.  
  61.     mov dword [num1], 0
  62.     mov dword [num2], 0
  63.  
  64.     push num1
  65.     push formatin
  66.     push esi
  67.     call fscanf
  68.     add esp, 12
  69.  
  70.     push num2
  71.     push formatin
  72.     push esi
  73.     call fscanf
  74.     add esp, 12
  75.  
  76.     jmp calculate
  77.  
  78. cmdline:
  79.  
  80.     push msg1
  81.     call printf
  82.  
  83.     push num1
  84.     push formatin
  85.     call scanf
  86.  
  87.     push msg2
  88.     call printf
  89.  
  90.     push num2
  91.     push formatin
  92.     call scanf
  93.  
  94. calculate:
  95.  
  96.     mov eax, dword [num1]
  97.     mov ebx, dword [num2]
  98.  
  99.     test eax, eax
  100.     jz error
  101.     test ebx, ebx
  102.     jz error
  103.  
  104. gcd0:
  105.  
  106.     xor edx, edx
  107.     div ebx
  108.     mov eax, ebx
  109.     mov ebx, edx
  110.     test ebx, ebx
  111.     jnz gcd0
  112.  
  113. end:
  114.  
  115.     push eax
  116.     push dword [num2]
  117.     push dword [num1]
  118.     push formatout
  119.     call printf
  120.  
  121.     cmp dword [isfilemode], 0
  122.     jne fileinput
  123.  
  124.     jmp exit
  125.  
  126. error:
  127.  
  128.     push errormsg
  129.     call printf
  130.  
  131. exit:
  132.  
  133.     mov eax, 1
  134.     mov ebx, 0
  135.     int 0x80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement