Advertisement
STANAANDREY

asm sum

Nov 3rd, 2024
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. section .data
  2.     prompt1 db 'Enter first number: ', 0
  3.     prompt1_len equ $ - prompt1
  4.     prompt2 db 'Enter second number: ', 0
  5.     prompt2_len equ $ - prompt2
  6.     result_msg db 'The sum is: ', 0
  7.     result_msg_len equ $ - result_msg
  8.     newline db 0xa
  9.    
  10. section .bss
  11.     num1 resb 6        ; Reserve 6 bytes for first number string
  12.     num2 resb 6        ; Reserve 6 bytes for second number string
  13.     sum resb 6         ; Reserve 6 bytes for sum string
  14.  
  15. section .text
  16.     global _start
  17.  
  18. _start:
  19.     ; Print first prompt
  20.     mov eax, 4
  21.     mov ebx, 1
  22.     mov ecx, prompt1
  23.     mov edx, prompt1_len
  24.     int 0x80
  25.  
  26.     ; Read first number
  27.     mov eax, 3
  28.     mov ebx, 0
  29.     mov ecx, num1
  30.     mov edx, 6
  31.     int 0x80
  32.  
  33.     ; Print second prompt
  34.     mov eax, 4
  35.     mov ebx, 1
  36.     mov ecx, prompt2
  37.     mov edx, prompt2_len
  38.     int 0x80
  39.  
  40.     ; Read second number
  41.     mov eax, 3
  42.     mov ebx, 0
  43.     mov ecx, num2
  44.     mov edx, 6
  45.     int 0x80
  46.  
  47.     ; Convert string to integer and add
  48.     mov esi, num1
  49.     call atoi          ; Convert first number to integer (result in eax)
  50.     mov ebx, eax       ; Save first number in ebx
  51.     mov esi, num2
  52.     call atoi          ; Convert second number to integer (result in eax)
  53.     add eax, ebx       ; Add the numbers
  54.  
  55.     ; Convert result back to string
  56.     mov esi, sum
  57.     call itoa
  58.  
  59.     ; Print result message
  60.     mov eax, 4
  61.     mov ebx, 1
  62.     mov ecx, result_msg
  63.     mov edx, result_msg_len
  64.     int 0x80
  65.  
  66.     ; Print sum
  67.     mov eax, 4
  68.     mov ebx, 1
  69.     mov ecx, sum
  70.     mov edx, 6
  71.     int 0x80
  72.  
  73.     ; Print newline
  74.     mov eax, 4
  75.     mov ebx, 1
  76.     mov ecx, newline
  77.     mov edx, 1
  78.     int 0x80
  79.  
  80.     ; Exit program
  81.     mov eax, 1
  82.     mov ebx, 0
  83.     int 0x80
  84.  
  85. ; Function to convert ASCII string to integer
  86. atoi:
  87.     xor eax, eax    ; Zero out eax
  88.     xor ebx, ebx    ; Zero out ebx
  89. .next_digit:
  90.     mov bl, [esi]   ; Get next character
  91.     cmp bl, 0xa     ; Check for newline
  92.     je .done
  93.     cmp bl, 0x20    ; Check for space
  94.     je .done
  95.     sub bl, '0'     ; Convert ASCII to number
  96.     imul eax, 10    ; Multiply current number by 10
  97.     add eax, ebx    ; Add new digit
  98.     inc esi         ; Move to next character
  99.     jmp .next_digit
  100. .done:
  101.     ret
  102.  
  103. ; Function to convert integer to ASCII string
  104. itoa:
  105.     push esi        ; Save esi
  106.     add esi, 5      ; Point to end of buffer
  107.     mov byte [esi], 0xa ; Add newline at end
  108.     mov ebx, 10     ; Divisor
  109. .next_digit:
  110.     dec esi         ; Move back one position
  111.     xor edx, edx    ; Clear edx for division
  112.     div ebx         ; Divide eax by 10
  113.     add dl, '0'     ; Convert remainder to ASCII
  114.     mov [esi], dl   ; Store ASCII digit
  115.     test eax, eax   ; Check if quotient is zero
  116.     jnz .next_digit ; If not zero, continue
  117.     pop esi         ; Restore esi
  118.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement