fork download
  1. section .data
  2. prompt db "Enter a number: ", 0
  3. resultMsg db "The sum is: ", 0
  4.  
  5. section .bss
  6. num1 resb 1
  7. num2 resb 1
  8. result resb 1
  9.  
  10. section .text
  11. global _start
  12.  
  13. _start:
  14. ; Print "Enter a number:"
  15. mov eax, 4 ; syscall number for sys_write
  16. mov ebx, 1 ; file descriptor for stdout
  17. mov ecx, prompt ; address of the prompt string
  18. mov edx, 16 ; length of the string
  19. int 0x80 ; invoke system call
  20.  
  21. ; Read first number from user
  22. mov eax, 3 ; syscall number for sys_read
  23. mov ebx, 0 ; file descriptor for stdin
  24. mov ecx, num1 ; address to store input
  25. mov edx, 1 ; read 1 byte
  26. int 0x80 ; invoke system call
  27.  
  28. ; Convert the first ASCII input to integer
  29. mov al, [num1] ; move the character from input to AL
  30. sub al, '0' ; convert ASCII to integer
  31. mov bl, al ; store the first number in bl
  32.  
  33. ; Print "Enter a number:"
  34. mov eax, 4
  35. mov ebx, 1
  36. mov ecx, prompt
  37. mov edx, 16
  38. int 0x80
  39.  
  40. ; Read second number from user
  41. mov eax, 3
  42. mov ebx, 0
  43. mov ecx, num2
  44. mov edx, 1
  45. int 0x80
  46.  
  47. ; Convert the second ASCII input to integer
  48. mov al, [num2]
  49. sub al, '0'
  50. mov cl, al ; store the second number in cl
  51.  
  52. ; Add the numbers
  53. add bl, cl ; bl = bl + cl
  54.  
  55. ; Print "The sum is: "
  56. mov eax, 4
  57. mov ebx, 1
  58. mov ecx, resultMsg
  59. mov edx, 15
  60. int 0x80
  61.  
  62. ; Convert the result to ASCII and print it
  63. add bl, '0' ; convert back to ASCII
  64. mov eax, 4 ; syscall number for sys_write
  65. mov ebx, 1 ; file descriptor for stdout
  66. mov ecx, result ; address of result variable
  67. mov [result], bl ; store the result in memory
  68. mov edx, 1 ; print 1 byte
  69. int 0x80 ; invoke system call
  70.  
  71. ; Exit the program
  72. mov eax, 1 ; syscall number for sys_exit
  73. xor ebx, ebx ; exit code 0
  74. int 0x80
  75.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Enter a number: Enter a number: The sum is: