Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. ;read a number in decimal, convert it to hex and print it on the fuckin screen
  2.  
  3. assume cs:code, ds: data
  4.  
  5. data segment
  6.     nrten db 10,10 dup(?)
  7.     lten db ?
  8.     nr dw ? ; the number in base ten
  9.     temp dw ?
  10.     invhex db 10 dup (?)
  11.     hextable db "0123456789ABCDEF"
  12.     result db 10 dup (?)
  13.     newline db 10,13,'$'
  14. data ends
  15.  
  16. code segment
  17.     start:
  18.         mov ax,data
  19.         mov ds,ax
  20.        
  21.         ;read the number in decimal
  22.         mov ah,0Ah
  23.         mov dx,offset nrten
  24.         int 21h
  25.        
  26.         mov al, nrten[1]
  27.         mov lten,al
  28.         ;mov si,lten
  29.         ;mov nrten[si+1],'$'
  30.        
  31.         mov cl, lten ;move the length of the string in cx
  32.         mov ch,0
  33.        
  34.        
  35.         mov temp,1
  36.         dec cx
  37.        
  38.         mulme:
  39.             ; multiply temp with 10
  40.             mov ax,temp
  41.             mov dx,10
  42.             mul dx ; ax := temp * 10
  43.             mov temp,ax
  44.         loop mulme
  45.        
  46.         ;now we have the power of then corresponding to the length of the string
  47.        
  48.         mov si,2
  49.         mov nr,0
  50.         mov cl, lten ;move the length of the string in cx
  51.         mov ch,0
  52.        
  53.         repme:
  54.             sub nrten[si],'0'
  55.    
  56.             mov ax,temp ; temp can be 1,10,100 ... whatever
  57.             mul nrten[si] ; ax := nrten[si] * ax
  58.            
  59.             add nr,ax  ;add the new digit to the number
  60.            
  61.             ; divide temp with 10
  62.             mov ax,temp
  63.             mov dl,10
  64.             div dl ; al := temp / 10
  65.             mov temp,ax
  66.             inc si         
  67.  
  68.         loop repme
  69.        
  70.         ;we should have the number in nr
  71.         ;convert the bitch to hexa
  72.        
  73.         mov si,0
  74.        
  75.         convertme:
  76.            
  77.             mov ax,nr
  78.             mov dl,16
  79.             div dl ; al := nr / 16 ah = nr mod 16
  80.             mov dl,al ; store the quotient, we will need it
  81.            
  82.             lea bx,hextable
  83.             mov al,ah
  84.             xlat
  85.            
  86.             ;al now has the ASCII hex digit
  87.             ;we put it in a string (we'll invert it later)
  88.            
  89.             mov invhex[si],al
  90.             inc si
  91.            
  92.             ;we still have the quotient in dl
  93.             ;we overwrite the number with it
  94.             mov dh,0
  95.             mov nr,dx
  96.            
  97.             cmp nr,0
  98.             jne convertme
  99.    
  100.         ;now invert the invhex string and get this over with
  101.         ;we have it's length in si
  102.  
  103.         dec si
  104.         mov cx,si
  105.         inc cx; si goes from 0
  106.         mov di,0
  107.        
  108.         invertme:
  109.             mov al,invhex[si]
  110.             mov result[di],al
  111.             dec si
  112.             inc di
  113.         loop invertme
  114.        
  115.         mov result[di],'$'
  116.        
  117.         ;print newline
  118.        
  119.         mov ah,09h
  120.         lea dx, newline
  121.         int 21h
  122.        
  123.         ;print it on the screen
  124.        
  125.         mov ah,09h
  126.         lea dx, result
  127.         int 21h
  128.        
  129.         ;wait for keyboard
  130.         mov ah,1h
  131.         int 21h
  132.        
  133.        
  134.         mov ax,4C00h
  135.         int 21h
  136. code ends
  137. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement