Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. org 0x7c00
  2. jmp 0x0000:start
  3.  
  4. notmsg db "Nao forma triangulo", 0
  5. eqmsg db "Equilatero", 0
  6. isomsg db "Isosceles", 0
  7. scamsg db "Escaleno", 0
  8. strinput db "000", 0
  9. X times 1 db 0
  10. Y times 1 db 0
  11. Z times 1 db 0
  12.  
  13. start: 
  14.     xor ax, ax      ; reg init
  15.     mov ds, ax      ; reg init
  16.     mov es, ax      ; reg init 
  17.     mov ss, ax      ; stack init
  18.     mov sp, 0x7c00      ; stack init
  19.  
  20.     mov di, strinput
  21.     call readvstr
  22.    
  23.     mov si, strinput
  24.     call atoi
  25.     mov byte [X], dl    ; and store the output conversion
  26.  
  27.     mov di, strinput
  28.     call readvstr
  29.    
  30.     mov si, strinput
  31.     call atoi
  32.     mov byte [Y], dl    ; and store the output conversion
  33.  
  34.     mov di, strinput
  35.     call readvstr
  36.    
  37.     mov si, strinput
  38.     call atoi
  39.     mov byte [Z], dl    ; and store the output conversion
  40.  
  41. ;;; [Checking if the vertices form a triangle] ;;;
  42. ;; X < (Y + Z) ;;
  43.     mov bl, byte [X]    ; check if X < Y + Z
  44.     mov al, byte [Y]
  45.     mov ah, byte [Z]
  46.     add al, ah      ; (Y + Z)
  47.     cmp bl, al      ; X ? (Y + Z)
  48.     jae .nottriangle    ; if (X >= Y + Z), then its not a triangle
  49. ;; Y < (X + Z) ;;
  50.     mov bl, byte [Y]
  51.     mov al, byte [X]
  52.     mov ah, byte [Z]
  53.     add al, ah      ; (X + Z)
  54.     cmp bl, al      ; Y ? (X + Z)
  55.     jae .nottriangle    ; if (Y >= X + Z), then its not a triangle
  56. ;; Z < (X + Y) ;;
  57.     mov bl, byte [Z]
  58.     mov al, byte [X]
  59.     mov ah, byte [Y]
  60.     add al, ah      ; (X + Y)
  61.     cmp bl, al      ; Z ? (X + Y)
  62.     jae .nottriangle    ; if (Z >= X + Y), then its not a triangle
  63. ;;; [Done] ;;;
  64.  
  65. ;;; [Check the type of triangle] ;;;
  66.     mov al, byte[X]
  67.     mov ah, byte[Y]
  68.     mov bl, byte[Z]
  69.    
  70.     cmp al, ah
  71.     jne .notequilateral
  72.     cmp al, bl
  73.     jne .isosceles
  74.     jmp .equilateral
  75.  
  76. .notequilateral:
  77.     cmp al, bl
  78.     je .isosceles
  79.     cmp ah, bl
  80.     je .isosceles
  81.     jmp .scalene
  82.    
  83. .equilateral:
  84.     mov si, eqmsg
  85.     jmp .done
  86.  
  87. .isosceles:
  88.     mov si, isomsg
  89.     jmp .done
  90.  
  91. .scalene:
  92.     mov si, scamsg
  93.     jmp .done
  94.    
  95. .nottriangle:
  96.     mov si, notmsg
  97.     jmp .done
  98.  
  99. .done:
  100.     call printstr
  101.     call println
  102.     jmp done
  103.  
  104. ;;; read (verbosely) string from di and print char by char
  105. ;; @param: use di to read  
  106. ;; @reg: ax, bx
  107. readvstr:      
  108.     .read:
  109.         mov ah, 0   ; read keystroke
  110.         int 16h     ; keyboard interrupt
  111.  
  112.         cmp al, 0xd     ; compare al with 'enter'
  113.         je .done
  114.    
  115.         stosb
  116.         jmp .print
  117.    
  118.     .print:
  119.         mov ah, 0xe     ; call number
  120.         mov bh, 0   ; page number
  121.         mov bl, 0xf ; white color
  122.         int 10h
  123.  
  124.         jmp .read
  125.  
  126.     .done:
  127.         call println    ; print line
  128.         mov al, 0   ; insert '\0'
  129.         stosb
  130.    
  131.         ret         ; return
  132.  
  133. ;;; string to integer -- int atoi(string*)
  134. ;; @param use si as string
  135. ;; @return dl as int result
  136. ;; @reg: ax, dx, bl, si
  137. atoi:
  138.     xor ax, ax      ; init
  139.     mov dx, ax
  140. .convert:  
  141.     lodsb
  142.  
  143.     cmp al, '0'
  144.     jb .done        ; character below '0'
  145.    
  146.     cmp al, '9'
  147.     ja .done        ; character above '9'
  148.  
  149.     sub al, '0'     ; convert ascii to (0-9) int
  150.  
  151.     xchg dl, al         ; this swap is needed because mul
  152.  
  153.     mov bl, 10      ; supose 12 from 123 string was computed, then 123 = (12*10) + 3
  154.     mul bl          ; prepare data for next unit digit
  155.     add dl, al      ; insert new digit into data
  156.    
  157.     jmp .convert
  158.    
  159. .done:
  160.     ret
  161.  
  162. ;;; print string
  163. ;; @param: use si to print
  164. ;; @reg: ax, bx
  165. printstr:
  166.     .start:
  167.  
  168.         lodsb       ; si -> al
  169.         cmp al, 0
  170.         je .done    ; if (end of string) return
  171.         jmp .print  ; else print current char
  172.  
  173.         .print:
  174.             mov ah, 0xe     ; print char and move cursor foward
  175.             mov bh, 0   ; page number
  176.             mov bl, 0xf     ; white color
  177.             int 10h     ; video interrupt
  178.  
  179.             jmp .start
  180.         .done:
  181.             ret
  182.  
  183. ;;; print line (\n)
  184. ;; @reg: ax, bx
  185. println:
  186.     mov ah, 0xe ; char print
  187.     mov bh, 0 ; page number
  188.     mov bl, 0xf ; white color
  189.     mov al, 13 ; vertical tab
  190.     int 10h ; visual interrupt
  191.    
  192.     mov ah, 0xe ; char print
  193.     mov bh, 0 ; page number
  194.     mov bl, 0xf ; white color
  195.     mov al, 10 ; backspace
  196.     int 10h ; visual interrupt 
  197.  
  198.     ret
  199.  
  200. ;;; integer to string -- string to_string(int*)
  201. ;; @param use ax as number input
  202. ;; @return di as string output
  203. ;; @reg: ax, bl, sp, di
  204. tostring:
  205.    
  206.     push 0          ; push '\0' end of string
  207.  
  208. .convert:           ; convert every digit of integer input into characters
  209.    
  210.     mov bl, 10      ; let number = 123, then, after div, 12 will be al, and 3 will be ah
  211.     div bl          ; so, we need to push 3 onto stack and recursively convert (number/10) until the result be zero
  212.     add ah, '0'     ; convert remainder to ascii...
  213.  
  214.     mov dl, ah      ; (although the remainder is stored to ah, the stosb works with al)
  215.     push dx         ; ...and push it   
  216.  
  217.     cmp al, 0       ; base case condition
  218.     je .concat
  219.    
  220.     mov ah, 0       ; the remainder was pushed onto stack, we dont need it anymore so AX = [3, 12] -> [0, 12]
  221.     jmp .convert
  222.    
  223. .concat:            ; concat every char of stack into a string
  224.    
  225.     pop ax          ; get top of stack and pop it
  226.    
  227.     stosb           ; store al at di
  228.    
  229.     cmp al, 0       ; if end of string
  230.     je .done        ; goto done
  231.     jmp .concat
  232.    
  233. .done:
  234.     ret
  235.  
  236.  
  237.    
  238. done:
  239.     jmp $           ; infinity jump
  240.  
  241. times 510-($-$$) db 0   ; Pad remainder of boot sector with 0s
  242. dw 0xAA55               ; The standard PC boot signature
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement