Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Format regex input: [0-9A-F]{4}\s[0-9A-F]{4}\n
  2. ; Format regex output: [0-9A-F]{4,5}
  3.  
  4. format MZ  
  5.  
  6. entry code_seg:start
  7.  
  8. stack 256
  9.  
  10. segment data_seg
  11. operand_1: db 0x0, 0x0
  12. operand_2: db 0x0, 0x0
  13.  
  14.      
  15. segment code_seg
  16. start:
  17.     mov ax, data_seg
  18.     mov ds, ax
  19.     xor dx, dx
  20.  
  21. read_input:
  22.     mov ah, 0x0
  23.     int 0x16
  24.  
  25. if_space:
  26.     cmp al, 0x20
  27.     je handle_space ; replace if_space to nop
  28.    
  29.     ; enter
  30.     cmp al, 0x0D
  31.     je handle_enter
  32.    
  33.     ; decimal number
  34.     cmp al, 0x30
  35.     jl read_input ; skip because less than 1
  36.     cmp al, 0x3A
  37.     jl handle_dec
  38.    
  39.     ; hexadecimal number
  40.     cmp al, 0x46
  41.     jg read_input ; skip because greater than 'F'
  42.     cmp al, 0x40
  43.     jg handle_hex
  44.    
  45.    
  46.     jmp read_input ; skip because unknown symbol
  47.  
  48.    
  49. ; input: al with byte
  50. ; output: concat_int
  51. handle_dec:
  52.     call print_char
  53.     sub al, 0x30
  54.     call concat_int
  55.     jmp read_input
  56.    
  57.    
  58. ; input: al with byte
  59. ; output: concat_int
  60. handle_hex:
  61.     call print_char  
  62.     sub al, 0x37
  63.     call concat_int
  64.     jmp read_input
  65.  
  66.  
  67. ; input: al with hex int
  68. ; output: dx int concatenate with hex int
  69. ;         change bx = 0x10
  70. concat_int:
  71.     mov bx, 0x10
  72.     xor ah, ah
  73.     push ax
  74.    
  75.     ; appending zero
  76.     mov ax, dx
  77.     mul bx
  78.     mov dx, ax
  79.    
  80.     ; concat dx number with al hex int
  81.     pop ax
  82.     add dx, ax    
  83.     ret
  84.    
  85.    
  86. ; input: al with byte
  87. ; output: void
  88. print_char:
  89.     mov ah, 0x0E
  90.     int 0x10
  91.     ret
  92.    
  93.    
  94. ; input: al with byte
  95. ; output: replace if_space to nop command
  96. ;         writing dx in operand_1
  97. ;         zeroing dx                
  98. handle_space:
  99.     call print_char
  100.    
  101.     mov ptr cs:if_space, word 0x9090
  102.     mov ptr cs:if_space + 2, word 0x9090
  103.     ;mov ptr cs:if_space + 4, byte 0x90
  104.    
  105.     mov word ptr operand_1, dx
  106.     xor dx, dx
  107.    
  108.     jmp read_input
  109.    
  110.    
  111. ; input: al with byte
  112. ; output: addition
  113. handle_enter:
  114.     push ax
  115.     mov al, 0x0A
  116.     call print_char
  117.     pop ax
  118.     call print_char
  119.     mov word ptr operand_2, dx
  120.     jmp addition
  121.  
  122. ;0110 1010 1001 0000
  123. ;0000 1001 1010 0110
  124. ; input: operand_1, operand_2 with word
  125. ; output: void
  126. addition:
  127.     mov ax, word ptr operand_1
  128.     mov dx, word ptr operand_2
  129.     add dx, ax
  130.    
  131.     jc print_1
  132.    
  133. end_print_1:
  134.     rol dl, 4
  135.     rol dh, 4
  136.     mov al, dh
  137.     mov ah, dl
  138.     xor dx, dx
  139.     mov bx, 0x10
  140.     mov cx, 4
  141.        
  142. print_sum:        
  143.     test cx, cx
  144.     je exit
  145.    
  146.     dec cx
  147.    
  148.     xor dx, dx
  149.     div bx
  150.    
  151.     cmp dl, 0xA
  152.     jl print_dec
  153.  
  154.     jmp print_hex
  155.  
  156.  
  157. ; input: void
  158. ; output: al with 0x31
  159. print_1:
  160.     mov al, 0x31
  161.     call print_char
  162.     jmp end_print_1
  163.  
  164.  
  165. ; input: dl with byte
  166. ; output: void
  167. print_dec:
  168.     push ax
  169.     mov al, dl
  170.     add al, 0x30
  171.     call print_char
  172.     pop ax
  173.     jmp print_sum
  174.  
  175. ; input: dl with byte
  176. ; output: void
  177. print_hex:
  178.     push ax
  179.     mov al, dl
  180.     add al, 0x37
  181.     call print_char
  182.     pop ax
  183.     jmp print_sum
  184.        
  185. exit:    
  186.     mov ax, 0x4C00
  187.     int 21h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement