Advertisement
milutinke

NASM Cancer

Apr 3rd, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. segment .code
  2.  
  3. _read_file:
  4.   ; Push all registers to the stack
  5.   pusha
  6.  
  7.   ; Open file
  8.   mov ah, 3dh
  9.   mov dx, settings_file
  10.   mov al, 0
  11.   int 21h
  12.  
  13.   ; Read file
  14.   mov bx, ax
  15.   mov ah, 3fh
  16.   mov cx, 256
  17.   mov dx, position_string
  18.   int 21h
  19.  
  20.   ; Close file
  21.   mov ah, 3eh
  22.   int 21h
  23.  
  24.   ; Parse text form the file and split it in two parts
  25.   mov si, position_string
  26.   call _split_string
  27.  
  28.   ; Covert row string to the integer and store it
  29.   mov si, position_row_string
  30.   call _string_to_int
  31.   mov [position_row_int], ax
  32.  
  33.   ; Covert columns string to the integer and store it
  34.   mov si, position_column_string
  35.   call _string_to_int
  36.   mov [position_column_int], ax
  37.  
  38.   ; Pop all registers from the stack
  39.   popa
  40. ret
  41.  
  42. _split_string:
  43.   ; Push all registers to the stack
  44.   pusha
  45.  
  46.   ; Clear ax and bx registers
  47.   xor ax, ax
  48.   xor bx, bx
  49.  
  50.   .loop:
  51.     mov al, byte [si]                             ; Load current character into the al
  52.  
  53.     ; Check if strings is terminated by zero and
  54.     ; current character is zero, if it is then exit out of the loop
  55.     or al, al                                     ; We can use cmp al, 0 but or al, al is faster
  56.     jz .end                                       ; Jump, exit out of the loop
  57.  
  58.     cmp al, ' '                                   ; Compare current character with space
  59.     je .next_string                               ; If current character is space then jump to the .next_string
  60.  
  61.     ; If delimiter has been found, copy only second string
  62.     cmp [.other_string], byte 1
  63.     je .second_string
  64.  
  65.     mov [position_row_string+bx], al              ; Add current letter to the first string
  66.     inc bx                                        ; Move position to the next character inside the current destination string
  67.     inc si                                        ; Move position to the next character inside string which needs to be parsed
  68.     jmp .loop                                     ; Loop again
  69.  
  70.     .second_string:
  71.       mov [position_column_string+bx], al         ; Add current letter to the second string
  72.       inc bx                                      ; Move position to the next character inside the current destination string
  73.       inc si                                      ; Move position to the next character inside string which needs to be parsed
  74.       jmp .loop                                   ; Loop again
  75.  
  76.     ; Copy only second string
  77.     .next_string:
  78.       mov [.other_string], byte 1                 ; Delimiter has been found, remember that we need to parse only second part of the string
  79.       xor bx, bx                                  ; Reset bx register, because we need to begin from the first place in new string (0)
  80.       inc si                                      ; Move position to the next character inside string which needs to be parsed
  81.       jmp .loop                                   ; Loop again
  82.  
  83.   ; Exit out of the loop
  84.   .end:
  85.  
  86.   ; Pop all registers from the stack
  87.   popa
  88. ret
  89.  
  90. segment .data
  91. .other_string: db 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement