Advertisement
Guest User

Untitled

a guest
Dec 30th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Progr           segment
  2.                 assume  cs:Progr, ds:dane, ss:stosik
  3.  
  4. start:          mov     ax,dane
  5.                 mov     ds,ax
  6.                 mov     ax,stosik
  7.                 mov     ss,ax
  8.                 mov     sp,offset szczyt
  9.  
  10.     next_digit:
  11.                 ; get char from keyboard
  12.                 ; into AL:
  13.                 MOV     AH, 00h
  14.                 INT     16h
  15.                 ; and print it:
  16.                 MOV     AH, 0Eh
  17.                 INT     10h
  18.  
  19.                 ; check for MINUS:
  20.                 CMP     AL, '-'
  21.                 JE      set_minus
  22.  
  23.                 ; check for ENTER key:
  24.                 CMP     AL, 13  ; carriage return?
  25.                 JNE     not_cr
  26.                 JMP     stop_input
  27.  
  28.     not_cr:
  29.                 jmp    backspace_checked
  30.  
  31.  
  32. backspace_checked:
  33.                 ; allow only digits:
  34.                 CMP     AL, '0'
  35.                 JAE     ok_AE_0
  36.  
  37. ok_AE_0:
  38.                 CMP     AL, '9'
  39.                 JBE     ok_digit
  40.                 ;todo skok że nie jest cyfrą
  41. ok_digit:
  42.                 ; multiply CX by 10 (first time the result is zero)
  43.                 PUSH    AX
  44.                 MOV     AX, CX
  45.                 MUL     CS:ten                  ; DX:AX = AX*10
  46.                 MOV     CX, AX
  47.                 POP     AX
  48.                 ; convert from ASCII code:
  49.                 SUB     AL, 30h
  50.                 ; add AL to CX:
  51.                 MOV     AH, 0
  52.                 ADD     CX, AX
  53.                                    ;todo jump if the number is too big.
  54.                 JMP     next_digit
  55.  
  56. set_minus:
  57.                 MOV     CS:make_minus, 1
  58.                 JMP     next_digit
  59.  
  60. stop_input:
  61.                 ; check flag:
  62.                 CMP     CS:make_minus, 0
  63.                 JE      print_hex
  64.                 NEG     CX
  65.                 jmp print_hex
  66.  
  67.  
  68.  
  69. print_hex:
  70.                 mov bl, cl      ;in cl is stored number
  71.                 mov cx, 4
  72.             .print_digit:
  73.                     rol bx, 4   ; move the currently left-most digit into the least significant 4 bits
  74.                     mov dx,bx
  75.                     and dx, 15  ; isolate the hex digit we want to print
  76.                     add dx,30h  ; and convert it into a character..
  77.                     cmp dx,'9'  ; ...
  78.                     jbe .ok     ; ...
  79.                     add dx,7    ; ... (for 'A'..'F')
  80.                 .ok:            ; ...
  81.                     mov ah,2h    ; INT 21H / AH=2: write character to stdout
  82.                     int 21h
  83.                     loop .print_digit
  84.  
  85.                     MOV AH, 4CH
  86.                     MOV AL, 01  ;your return code.
  87.                     INT 21H
  88.  
  89. Progr           ends
  90.  
  91. dane            segment
  92. make_minus      DB      ?       ; used as a flag.
  93. ten             DW      10      ; used as multiplier.
  94. dane            ends
  95.  
  96. stosik          segment
  97.                 dw    100h dup(0)
  98. szczyt          Label word
  99. stosik          ends
  100.  
  101. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement