Advertisement
STANAANDREY

(2A - 3B)/5C

Nov 4th, 2024 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. section .data
  2.   msg_zero_div db "ERROR: Zero division!", 10
  3.   len_zero_div equ $ - msg_zero_div
  4.   msg_negative db "Negative numerator", 10
  5.   len_negative equ $ - msg_negative
  6.   msg_zero db "0", 10
  7.   len_zero equ $ - msg_zero
  8.   msg_cat db "quo="
  9.   len_cat equ $ - msg_cat
  10.   msg_rest db 10, "rem="
  11.   len_rest equ $ - msg_rest
  12.   newline db 10
  13.  
  14. section .bss
  15.   a resb 2
  16.   b resb 2
  17.   c resb 2
  18.   quo resb 2
  19.   rem resb 2
  20.  
  21. section .text
  22. global _start
  23.  
  24. _start:
  25.   ; read A
  26.   mov eax, 3
  27.   mov ebx, 0
  28.   mov ecx, a
  29.   mov edx, 2
  30.   int 0x80
  31.  
  32.   ; read B
  33.   mov eax, 3
  34.   mov ebx, 0
  35.   mov ecx, b
  36.   mov edx, 2
  37.   int 0x80
  38.  
  39.   ; read C
  40.   mov eax, 3
  41.   mov ebx, 0
  42.   mov ecx, c
  43.   mov edx, 2
  44.   int 0x80
  45.  
  46.   ; compute
  47.   movzx eax, byte [a]
  48.   sub eax, '0'
  49.   mov ebx, 2
  50.   mul ebx       ; 2A
  51.  
  52.   mov ebx, eax    
  53.  
  54.   movzx eax, byte [b]
  55.   sub eax, '0'
  56.   mov ecx, 3
  57.   mul ecx         ; 3B
  58.  
  59.   sub ebx, eax    
  60.  
  61.   ; if quo is 0
  62.   cmp ebx, 0
  63.   je zero_result
  64.  
  65.   ; if quo < 0
  66.   cmp ebx, 0
  67.   jl negative_result
  68.  
  69.   movzx ecx, byte [c]
  70.   sub ecx, '0'
  71.   mov eax, 5
  72.   mul ecx         ; 5C
  73.  
  74.   ; if zero div
  75.   cmp eax, 0
  76.   je zero_division
  77.  
  78.   mov ecx, eax    
  79.   mov eax, ebx    
  80.   cdq            
  81.   div ecx        
  82.  
  83.   ; print quo
  84.   add eax, '0'
  85.   mov [quo], al
  86.  
  87.   mov eax, 4
  88.   mov ebx, 1
  89.   mov ecx, msg_cat
  90.   mov edx, len_cat
  91.   int 0x80
  92.  
  93.   mov eax, 4
  94.   mov ebx, 1
  95.   mov ecx, quo
  96.   mov edx, 1
  97.   int 0x80
  98.  
  99.   ; print quo
  100.   mov eax, edx    
  101.   add eax, '0'
  102.   mov [rem], al
  103.  
  104.   mov eax, 4
  105.   mov ebx, 1
  106.   mov ecx, msg_rest
  107.   mov edx, len_rest
  108.   int 0x80
  109.  
  110.   mov eax, 4
  111.   mov ebx, 1
  112.   mov ecx, rem
  113.   mov edx, 1
  114.   int 0x80
  115.  
  116.   mov eax, 4
  117.   mov ebx, 1
  118.   mov ecx, newline
  119.   mov edx, 1
  120.   int 0x80
  121.  
  122.   jmp exit
  123.  
  124. zero_division:
  125.   mov eax, 4
  126.   mov ebx, 1
  127.   mov ecx, msg_zero_div
  128.   mov edx, len_zero_div
  129.   int 0x80
  130.   jmp exit
  131.  
  132. negative_result:
  133.   mov eax, 4
  134.   mov ebx, 1
  135.   mov ecx, msg_negative
  136.   mov edx, len_negative
  137.   int 0x80
  138.   jmp exit
  139.  
  140. zero_result:
  141.   mov eax, 4
  142.   mov ebx, 1
  143.   mov ecx, msg_zero
  144.   mov edx, len_zero
  145.   int 0x80
  146.   jmp exit
  147.  
  148. exit:
  149.   mov eax, 1
  150.   xor ebx, ebx
  151.   int 0x80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement