fork download
  1. ; Assemblatore NASM 2.07
  2. ;
  3. ; convert a decimal ASCII string in a 16-bit unsigned int
  4. ; the string is read from std input
  5. ;
  6. global _start
  7.  
  8. section .data
  9. buffer resb 32
  10. string resb 32
  11.  
  12. msg db 'the char pointed to by ESI is: '
  13. number db '0000'
  14. db 0ah,0
  15.  
  16. section .text
  17.  
  18. _start:
  19. mov ecx,buffer
  20. mov edx,32
  21. call read
  22.  
  23. mov al,0ah
  24. mov edx,buffer
  25. call strlen ; ECX=string lenght
  26. mov byte [buffer+ecx],0 ; make ASCIIZ
  27. mov edx,buffer
  28. call str2uint16 ; AX=binary number
  29.  
  30. mov edx,string
  31. call bin2dec16 ; EXC=length
  32. mov byte [edx+ecx],0ah
  33. mov byte [edx+ecx+1],0
  34. call printasciiz
  35.  
  36. ; now print the char pointed to by ESI (in hex)
  37.  
  38. mov al,[esi]
  39. xor ah,ah
  40. mov edx,number
  41. call bin2hex16
  42. mov edx,msg
  43. call printasciiz
  44.  
  45. exit:
  46. mov eax, 01h ; exit()
  47. xor ebx, ebx ; errno
  48. int 80h
  49.  
  50.  
  51.  
  52. ; FUNCTION: str2uint16 (V0.2)
  53. ; converte una stringa ASCIIZ decimale in un numero intero
  54. ; a 16 bit senza segno
  55. ;
  56. ; Parametri:
  57. ; EDX = indirizzo del buffer ASCII
  58. ; Return:
  59. ; AX = numero binario 16 bit senza segno
  60. ; ESI = puntatore al primo char non valido (oppure al NULL)
  61. ;
  62. section .data
  63. pow10_16 dw 1,10,100,1000,10000
  64.  
  65. section .text
  66. str2uint16:
  67. push ebx
  68. push ecx
  69. push edx
  70. push edi
  71. mov esi,edx
  72. mov ecx,0 ; contatore digits sullo stack
  73. .loop1:
  74. xor ah,ah
  75. mov al,[esi] ; carica la cifra decimale
  76. cmp al,0
  77. je .end
  78. cmp al,'0'
  79. jb .notvalid
  80. cmp al,'9'
  81. ja .notvalid
  82. sub al,30h
  83. inc esi
  84. push ax
  85. inc ecx
  86. cmp ecx,5
  87. jbe .loop1
  88. .overflow:
  89. pop ax
  90. dec ecx ; converte solo le prime 5 cifre
  91. .notvalid:
  92. .end:
  93. mov di,0 ; totalizzatore
  94. mov ebx,pow10_16
  95. cmp ecx,0
  96. je .exit
  97. .loop2:
  98. pop ax
  99. mov dx,[ebx] ; carica potenza del 10
  100. mul dx ; mult cifra per potenza del 10 dx:ax
  101. add di,ax
  102. inc ebx
  103. inc ebx
  104. loop .loop2
  105. .exit:
  106. mov ax,di
  107. pop edi
  108. pop edx
  109. pop ecx
  110. pop ebx
  111. ret
  112.  
  113. ; FUNCTION: bin2dec16
  114. ; converte un numero binario a 16 bit in una stringa ASCII decimale
  115. ; Parametri:
  116. ; AX = numero da stampare
  117. ; EDX = indirizzo del buffer ASCII (5 cifre)
  118. ; Return:
  119. ; ECX = lunghezza della strinag
  120. bin2dec16:
  121. push eax
  122. push ebx
  123. push edx
  124. push esi
  125. mov esi,edx
  126. mov ecx,0 ; contatore cifre significative
  127. mov bx,10 ; divisore
  128. .loop1:
  129. xor dx,dx ; DX:AX = dividendo
  130. div bx ; ax=quoziente, dx=resto
  131. add dl,30h
  132. push dx ; salva il digit sullo stack
  133. inc ecx ; incrementa contatore cifre
  134. cmp ax,0
  135. jne .loop1
  136.  
  137. mov eax,ecx ; salva numero digits
  138.  
  139. .loop2: ;recupera i digit dallo stack in ordine inverso (almeno 1)
  140. pop dx
  141. mov [esi],dl
  142. inc esi
  143. loop .loop2
  144. mov ecx,eax ; return ECX
  145. pop esi
  146. pop edx
  147. pop ebx
  148. pop eax
  149. ret
  150.  
  151. ; FUNCTION: bin2hex16
  152. ; converte il numero a 16 bit in AX in 4 cifre hex
  153. ; Parametri:
  154. ; AX = numero binario
  155. ; EDX = buffer di 4 cifre hex (es: XXXX)
  156. ; Return:
  157. ; none
  158. bin2hex16:
  159. push eax
  160. push ebx
  161. push ecx
  162. push edx
  163. push edi
  164. mov edi,edx ; usa edi per indirizzare il buffer
  165. add edi,3
  166. mov cx,4
  167. .loop1:
  168. xor dx,dx ; DX:AX = dividendo
  169. mov bx,16 ; divisore
  170. div bx ; AX=quoziente, DX=resto
  171. add dl,30h
  172. cmp dl,'9'
  173. jbe .digit
  174. add dl,7
  175. .digit:
  176. mov [edi],dl
  177. dec edi
  178. loop .loop1
  179.  
  180. pop edi
  181. pop edx
  182. pop ecx
  183. pop ebx
  184. pop eax
  185. ret
  186.  
  187.  
  188. ; FUNCTION: printasciiz
  189. ; stampa la stringa ASCIIZ in ingresso. La stringa è terminata da un NULL byte
  190. ; Parametri:
  191. ; EDX = indirizzo della stringa ASCIIZ
  192. ; Return:
  193. ; none
  194. ;
  195. printasciiz:
  196. push eax
  197. push ecx
  198. push edx
  199. mov al,0
  200. call strlen ; ecx=length
  201. mov eax,ecx ; scambia ecx ed edx
  202. mov ecx,edx ; indirizzo buffer in ecx
  203. mov edx,eax ; length in edx
  204. call write
  205. pop edx
  206. pop ecx
  207. pop eax
  208. ret
  209.  
  210.  
  211. ; FUNCTION: strlen
  212. ; calcola la lunghezza della stringa terminata dal char AL
  213. ; Parametri:
  214. ; AL = carattere terminatore
  215. ; EDX = indirizzo della stringa
  216. ; Return:
  217. ; ECX = lunghezza stringa
  218. strlen:
  219. push eax
  220. push edx
  221. push esi
  222. mov esi,edx ; EDX=start string
  223. .loop1:
  224. mov ah,[esi]
  225. cmp ah,al
  226. je .end
  227. inc esi
  228. jmp .loop1
  229. .end:
  230. mov ecx,esi ; ECX=end string
  231. sub ecx,edx ; sottrae dalla fine del buffer l'inizio
  232. pop esi
  233. pop edx
  234. pop eax
  235. ret
  236.  
  237.  
  238. ; FUNCTION: read
  239. ; legge un buffer da standard input
  240. ; Parametri:
  241. ; ECX = indirizzo del buffer
  242. ; EDX = lunghezza del buffer
  243. ;
  244. read:
  245. push eax
  246. push ebx
  247. mov eax, 03h ; read()
  248. mov ebx, 00h ; stdin
  249. int 80h
  250. pop ebx
  251. pop eax
  252. ret
  253.  
  254. ; FUNCTION: write
  255. ; scrive un buffer su standard input
  256. ; Parametri:
  257. ; ECX = indirizzo del buffer
  258. ; EDX = lunghezza del buffer
  259. ;
  260. write:
  261. push eax
  262. push ebx
  263. mov eax, 04h ; write()
  264. mov ebx, 01h ; stdout
  265. int 80h
  266. pop ebx
  267. pop eax
  268. ret
  269.  
  270.  
Success #stdin #stdout 0.01s 144KB
stdin
65535
stdout
65535