Question

Hello i need some help regarding something, In assembly, the number you input into the ecx register, are they in the form of ascii or decimal. and lets say i wanted to add two or more numbers inputted numbers up, I would have to change the the input to decimal right

section .data
msg db "Enter your numbers", 0xA,0xD 
len equ $-msg
size dd "0: "
lens equ $-size

section .bss
num resd 3
sum resd 3

section .text
global  _start
_start:

;print msg
mov eax,4
mov ebx,1
mov ecx, msg
mov edx, len
int 0X80


;start loop

top:
;print out at what input you have reached 
mov eax,4
mov ebx,1
mov ecx,size
mov edx,lens
int     0X80

;enter a number in num
mov eax,3
mov ebx,1
mov ecx,num
mov edx,3
int 0X80
;print out the number you have entered in num
mov eax, 4
mov ebx,1
mov ecx,num
mov edx,3
int 0X80


mov eax, [num]
add eax, 0
mov ebx, [sum]
add ebx, 0
add ebx, eax
add ebx, "0"
mov [sum], ebx

mov eax, 4
mov ebx, 1
mov ecx, [sum]
mov edx, 3
int     0X80

;increment the input
mov eax, [size]
add eax, 1
mov [size],eax
cmp eax, "9: "
jle top


mov eax,1
int 0X80
Was it helpful?

Solution

sys_read doesn't put anything "into the ecx register". It puts text into a buffer pointed to by ecx. It is ascii text, yes. The "enter" key (linefeed = 10 decimal = 0Ah) which ends input is included (so you've only got room for two digits). Given a well-behaved user, we may hope that it is an ascii representation of a decimal number. Might be a representation of a hex number... or might not be a number at all - depends on what the user enters.

To do arithmetic on this, you're going to have to convert it to a number. The computer happens to store this as binary, but think of it as "a number". "Decimal", "hex", etc. are ways of representing a number but it's the same number (linefeed, for example is 10 or 0Ah). If the user enters "42", assuming we mean this as decimal, thats 4 * 10 + 2, right? The character '4' is not the number 4, but can be converted by subtracting '0' (48 decimal or 30h - not the number 0!). Don't try to convert the linefeed, it's not part of our number.

After your calculation, you'll have to convert the number back into text before you can print it (presumably a decimal representation of the number - but you could print the result as hex... or octal or binary or something else).

This is about the most commonly asked question of all time, so you should be able to find examples.

You were doing pretty good up until add eax, 0, which doesn't do anything (except set flags). You'll want to be working with single characters (one byte or 8 bits) for the "text" part, although the "number" could be bigger.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top