fork download
  1. section .data
  2. prompt db "Enter a number (0 to exit): ", 0
  3. binary db 10, "Binary: ", 0
  4. newline db 10, 0
  5.  
  6. section .bss
  7. input resb 10
  8.  
  9. section .text
  10. global _start
  11.  
  12. _start:
  13. ; Display prompt
  14. mov eax, 4
  15. mov ebx, 1
  16. mov ecx, prompt
  17. mov edx, 26
  18. int 0x80
  19.  
  20. ; Read input
  21. mov eax, 3
  22. mov ebx, 0
  23. mov ecx, input
  24. mov edx, 10
  25. int 0x80
  26.  
  27. ; Null-terminate input safely
  28. mov esi, eax
  29. dec esi
  30. mov byte [input + esi], 0
  31.  
  32. ; Convert input string to integer
  33. mov esi, input
  34. call str_to_int
  35. cmp eax, 0
  36. je exit
  37. mov ebx, eax
  38.  
  39. ; Display "Binary:"
  40. mov eax, 4
  41. mov ebx, 1
  42. mov ecx, binary
  43. mov edx, 9
  44. int 0x80
  45.  
  46. ; Print binary representation
  47. call print_binary
  48.  
  49. ; Print newline
  50. mov eax, 4
  51. mov ebx, 1
  52. mov ecx, newline
  53. mov edx, 1
  54. int 0x80
  55.  
  56. jmp _start
  57.  
  58. exit:
  59. mov eax, 1
  60. xor ebx, ebx
  61. int 0x80
  62.  
  63. ; -----------------------------------------
  64. ; Convert string at [esi] to integer in eax
  65. str_to_int:
  66. xor eax, eax
  67. xor ecx, ecx
  68. .loop:
  69. mov cl, byte [esi]
  70. cmp cl, 0
  71. je .done
  72. sub cl, '0'
  73. imul eax, eax, 10
  74. add eax, ecx
  75. inc esi
  76. jmp .loop
  77. .done:
  78. ret
  79.  
  80. ; -----------------------------------------
  81. ; Print binary representation of EBX
  82. print_binary:
  83. mov eax, ebx
  84. mov ecx, 32
  85. mov edx, 0
  86. mov esi, 1
  87. shl esi, 31 ; Highest bit mask
  88. mov edi, 0 ; Flag: first 1 printed
  89.  
  90. .loop:
  91. test eax, esi
  92. jz .maybe_skip
  93. mov edi, 1
  94. mov dl, '1'
  95. jmp .print
  96.  
  97. .maybe_skip:
  98. cmp edi, 0
  99. je .skip
  100. mov dl, '0'
  101.  
  102. .print:
  103. mov [input], dl
  104. mov eax, 4
  105. mov ebx, 1
  106. mov ecx, input
  107. mov edx, 1
  108. int 0x80
  109.  
  110. .skip:
  111. shr esi, 1
  112. dec ecx
  113. jnz .loop
  114. ret
  115.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Enter a number (0 to exit)