0

How can I print the following on one line?

The sum of <num> and <num2> is <sum>

Instead, it's printing on three lines:

Enter a number: 1
Enter a second number: 2
The sum of 1
    and 2
    equals 3

Here is my code:

section .data                           ;Data segment
   userMsg db 'Enter a number: ' ;Ask the user to enter a number
   lenUserMsg equ $-userMsg             ;The length of the message
   userMsg2 db 'Enter a second number: ' ;Ask the user to enter a number
   lenUserMsg2 equ $-userMsg2             ;The length of the message
   dispMsg db 'The sum of '
   lenDispMsg equ $-dispMsg         
   dispMsg2 db ' and '
   lenDispMsg2 equ $-dispMsg2
   dispMsg3 db ' equals '
   lenDispMsg3 equ $-dispMsg3

section .bss           ;Uninitialized data
   num resb 5
   num2 resb 5
   sum resb 5
    
section .text          ;Code Segment
   global _start
    
_start:                ;User prompt
   mov eax, 4
   mov ebx, 1
   mov ecx, userMsg
   mov edx, lenUserMsg
   int 80h

   ;Read and store the first user input
   mov eax, 3
   mov ebx, 2
   mov ecx, num  
   mov edx, 5          ;5 bytes (numeric, 1 for sign) of that information
   int 80h
   
   ;Second user input
   mov eax, 4
   mov ebx, 1
   mov ecx, userMsg2
   mov edx, lenUserMsg2
   int 80h

   ;Read and store the second user input
   mov eax, 3
   mov ebx, 2
   mov ecx, num2  
   mov edx, 5
   int 80h
    
   ;'The sum of '
   mov eax, 4
   mov ebx, 1
   mov ecx, dispMsg
   mov edx, lenDispMsg
   int 80h  
   
   ;num
   mov eax, 4
   mov ebx, 1
   mov ecx, num  
   mov edx, 5
   int 80h
   
   ;' and '
   mov eax, 4
   mov ebx, 1
   mov ecx, dispMsg2
   mov edx, lenDispMsg2
   int 80h  
   
   ;num2
   mov eax, 4
   mov ebx, 1
   mov ecx, num2  
   mov edx, 5
   int 80h
   
   ;' equals '
   mov eax, 4
   mov ebx, 1
   mov ecx, dispMsg3
   mov edx, lenDispMsg3
   int 80h  

   ;Add the inputs
   mov eax, [num]
   sub eax, '0' ;convert from ASCII to decimal
   mov ebx, [num2]
   sub eax, '0'
   add eax, ebx
   add eax, '0'
   mov [sum], eax

   ;Print the sum
   mov eax, 4
   mov ebx, 1
   mov ecx, sum
   mov edx, 5
   int 0x80
    
   ; Exit code
   mov eax, 1
   mov ebx, 0
   int 80h
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Josie
  • 1
  • 2
    It looks like a newline character is included in the data read and it should be removed before printing. – MikeCAT Mar 04 '23 at 22:45
  • You're passing length=5 to your `write` system calls, but your code only works with 1-digit numbers (using `sub eax, '0'`). If you use `strace ./a.out` to see the system calls your program makes, you can see `write(1, "2\n\0\0\0", 5) = 5`, and `write(1, "3\24\0\0\0", 5) = 5` from the output where you added two `0xa` newlines together to get `0x14` = 20 = octal 0o24 which strace uses by default for non-ASCII data. Also note the binary zeros your program writes to its stdout. – Peter Cordes Mar 05 '23 at 04:19
  • See [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) for multi-digit input, and [How do I print an integer in Assembly Level Programming without printf from the c library? (itoa, integer to decimal ASCII string)](https://stackoverflow.com/a/46301894) for multi-digit number output. You can optimize away the looping if you only want it to work with single-digit numbers; for your case it's probably just a matter of using your current code with length = 1 for the writes. – Peter Cordes Mar 05 '23 at 04:21

0 Answers0