I am working through some simple problems for practise, and one of the problems is to read in a number, and then output that number plus 1.
For example:
Please enter a number: 4
5
Now, with that input and output, it should be easy. But I want to make my program capable of processing multiple digit numbers. And that is where I am stumped. Because if I was given the input of 999, how could my program know to make the last 9 a 0, and add 1 to the tens column? And then, how could it know to make the number into 1000 by adding another digit?
Here is my code so far:
SECTION .data
msg db "Please enter a number: ",0
msglen EQU $-msg
sz EQU 32
NUL EQU 0
len EQU 32
SECTION .bss
num resb len
SECTION .text
GLOBAL _start
_start:
Msg: ; display the message
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, msglen
int 80h
Read: ; read in the number
mov eax, 3
mov ebx, 1
mov ecx, num
mov edx, len
int 80h
Length: ; Find length of string
mov eax, num
mov ebx, 0
loop:
cmp BYTE[eax], NUL
je Set
inc eax
inc ebx
jmp loop
Set: ; set up the registers and variables
mov ecx, num
Print: ; main section - prints out the number plus 1
cmp BYTE[ecx], NUL
je Exit
mov eax, 4
mov ebx, 1
mov edx, 1 ; NOTE- does not yet add 1 to the number
int 80h
inc ecx
jmp Print
Exit: ; exits safely
mov eax, 1
mov ebx, 0
int 80h
Could someone show me how to do this problem? I just can’t figure out the adding part. I can print out multiple digits, up to 32 bits, but the adding is just a mystery.
Thanks in advance,
Rileyh
How about using atoi() to convert it into the integers and then printf() to output the result? There are some sample codes about it:
http://cs.lmu.edu/~ray/notes/nasmexamples/
As for your integer-adding problem, if I were you, I’d start by tearing the problem down to “add one”. You’ll notice you’re following this algorithm there:
From this point, you’ll only need to figure out what changes if you want to add more than one digit of the input.