section .data
msg db 'Introduceti primul numar: '
lenMsg equ $ -msg
newline db 10
MSG db 'Introduceti al doilea numar: '
LENMSG equ $ -MSG
result db 'Suma este: '
lenResult equ $ -result
section .bss
num1 resb 1
num2 resb 1
sum resb 1
section .text
global _start
_start:
; Afisare mesaj 1
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, lenMsg
int 0x80
; Citire numar 1
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 1
int 0x80
; Afisare mesaj 2
mov eax, 4
mov ebx, 1
mov ecx, MSG
mov edx, LENMSG
int 0x80
; Citire numar 2
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 1
int 0x80
; Afisare ultim mesaj
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, lenResult
int 0x80
;afisare suma
mov eax, 4
mov ebx, 1
mov ecx, sum
mov edx, 1
int 0x80
; Afisare newline
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
; Iesire din program
mov eax, 1
xor ebx, ebx
int 0x80
how can i add the 2 numbers,any method i tried with movzx etc.,hasn t worked,i was printing nothing for sum
section .data
msg db 'Introduceti primul numar: '
lenMsg equ $ -msg
newline db 10
MSG db 'Introduceti al doilea numar: '
LENMSG equ $ -MSG
result db 'Suma este: '
lenResult equ $ -result
section .bss
num1 resb 1
num2 resb 1
sum resb 1
section .text
global _start
_start:
; Afisare mesaj 1
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, lenMsg
int 0x80
; Citire numar 1
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 1
int 0x80
; Afisare mesaj 2
mov eax, 4
mov ebx, 1
mov ecx, MSG
mov edx, LENMSG
int 0x80
; Citire numar 2
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 1
int 0x80
; Afisare ultim mesaj
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, lenResult
int 0x80
;afisare suma
mov eax, 4
mov ebx, 1
mov ecx, sum
mov edx, 1
int 0x80
; Afisare newline
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
; Iesire din program
mov eax, 1
xor ebx, ebx
int 0x80
how can i add the 2 numbers,any method i tried with movzx etc.,hasn t worked,i was printing nothing for sum
Share Improve this question edited Mar 28 at 10:31 Peter Cordes 367k49 gold badges717 silver badges980 bronze badges asked Mar 28 at 5:31 Alex BivolAlex Bivol 91 silver badge2 Answers
Reset to default 1You need procedures like ASCIItoInteger to convert a number from ASCII representation, which you'll get from Citire numar, to the binary form in register, which you can add using machine instructions.
And a similar procedure IntegerToASCII to convert the sum to ASCII characters which you can display with afisare suma.
See my tutorial for details.
i was printing nothing for sum
You probably did get one space character printed from the zero that was stored in sum by default.
num1 resb 1 num2 resb 1 sum resb 1
From these definitions one might deduce that you want to input two single-digit numbers, add them, and display their sum.
Since both num1 and num2 contain a character from "0" to "9", the first thing you could do is convert them into a digit from 0 to 9 by subtracting 48 (the ASCII code for "0"). These digits you can simply add and let's assume their sum stays below 10 (*). In order to print the result you need to convert the single digit back into a character by adding 48. Next code just simplifies this by skipping a step or two:
mov al, [num1]
sub al, "0"
add al, [num2]
mov [sum], al
;afisare suma
mov edx, 1 ; count
mov ecx, sum ; address
mov ebx, 1 ; std_out
mov eax, 4 ; system_write
int 0x80 ; make system call
(*) If the sum is bigger than 9, or if the inputted numbers are no longer single-digit, then conversion routines are needed. Both Inputting multi-radix multi-digit signed numbers with DOS and Displaying numbers with DOS have such conversion routines albeit for the DOS level. But don't worry, the principles haven't changed since...
Even for our single-digit inputs we still have to take the newline into account! eg. If you pressed 4enter5enter then num1 would hold ASCII 52 ("4"), num2 would hold ASCII 10 (the first enter), and the other keys would remain with the kernel.
A simple solution is to use two 2-byte buffers and ask accordingly but use only the first byte:
num1 resb 2
num2 resb 2
sum resb 1
...
; Citire numar 1
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 2 <<<
int 0x80
...
; Citire numar 2
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 2 <<<
int 0x80
Now pressing 4enter5enter will store in num1 ASCII 52 ("4") followed by ASCII 10 (the first enter), and in num2 ASCII 53 ("5") followed by ASCII 10 (the second enter).