Arithmetic operation in NASM showing arbitrary character












0














Below is my code on NASM. I am taking two integers as user input (2 and 3) and want to add them then print to stdout. Input is taking correctly but as output it is showing some arbitrary character. What I am doing wrong? I believe something is wrong in adding input block but not sure what it is.



section .bss
fnum rest 255
lnum rest 255

section .text
global _start
_start:
; first input
mov ecx, fnum
mov edx, 255
mov ebx, 0
mov eax, 3
int 80h

; second input
mov ecx, lnum
mov edx, 255
mov ebx, 0
mov eax, 3
int 80h

; adding inputs 2 and 3
mov eax, fnum
add eax, ecx
add eax, 48
push eax
mov eax, esp

;print output
mov ecx, eax
mov edx, 2
mov ebx, 1
mov eax, 4
int 80h









share|improve this question













migrated from unix.stackexchange.com Nov 19 '18 at 19:52


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.















  • The same arbitrary character every time, or different every time?
    – Kusalananda
    Nov 19 '18 at 19:35










  • same. something like this ▒, For simplicity I am taking inputs that will produce output less than 9.
    – al mamun
    Nov 19 '18 at 19:41






  • 1




    you'll need to reinvent printf("%dn", ...) to turn the contents of a register into a human-readable number, either by compiling in libc and calling printf or by doing it manually via something like github.com/thrig/scripts/blob/master/asm/Darwin/x86_64/…
    – thrig
    Nov 19 '18 at 19:51






  • 1




    You're printing the low 2 bytes of the sum of the addresses, not the sum of the ASCII characters at the pointed-to locations.
    – Peter Cordes
    Nov 19 '18 at 19:57












  • @thrig I think the adding 48 accomplishes that, but I don't see the equivalent subtracting 48 from the inputs (or, more simply, subtracting 48 once and never adding it).
    – Daniel H
    Nov 19 '18 at 20:00
















0














Below is my code on NASM. I am taking two integers as user input (2 and 3) and want to add them then print to stdout. Input is taking correctly but as output it is showing some arbitrary character. What I am doing wrong? I believe something is wrong in adding input block but not sure what it is.



section .bss
fnum rest 255
lnum rest 255

section .text
global _start
_start:
; first input
mov ecx, fnum
mov edx, 255
mov ebx, 0
mov eax, 3
int 80h

; second input
mov ecx, lnum
mov edx, 255
mov ebx, 0
mov eax, 3
int 80h

; adding inputs 2 and 3
mov eax, fnum
add eax, ecx
add eax, 48
push eax
mov eax, esp

;print output
mov ecx, eax
mov edx, 2
mov ebx, 1
mov eax, 4
int 80h









share|improve this question













migrated from unix.stackexchange.com Nov 19 '18 at 19:52


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.















  • The same arbitrary character every time, or different every time?
    – Kusalananda
    Nov 19 '18 at 19:35










  • same. something like this ▒, For simplicity I am taking inputs that will produce output less than 9.
    – al mamun
    Nov 19 '18 at 19:41






  • 1




    you'll need to reinvent printf("%dn", ...) to turn the contents of a register into a human-readable number, either by compiling in libc and calling printf or by doing it manually via something like github.com/thrig/scripts/blob/master/asm/Darwin/x86_64/…
    – thrig
    Nov 19 '18 at 19:51






  • 1




    You're printing the low 2 bytes of the sum of the addresses, not the sum of the ASCII characters at the pointed-to locations.
    – Peter Cordes
    Nov 19 '18 at 19:57












  • @thrig I think the adding 48 accomplishes that, but I don't see the equivalent subtracting 48 from the inputs (or, more simply, subtracting 48 once and never adding it).
    – Daniel H
    Nov 19 '18 at 20:00














0












0








0







Below is my code on NASM. I am taking two integers as user input (2 and 3) and want to add them then print to stdout. Input is taking correctly but as output it is showing some arbitrary character. What I am doing wrong? I believe something is wrong in adding input block but not sure what it is.



section .bss
fnum rest 255
lnum rest 255

section .text
global _start
_start:
; first input
mov ecx, fnum
mov edx, 255
mov ebx, 0
mov eax, 3
int 80h

; second input
mov ecx, lnum
mov edx, 255
mov ebx, 0
mov eax, 3
int 80h

; adding inputs 2 and 3
mov eax, fnum
add eax, ecx
add eax, 48
push eax
mov eax, esp

;print output
mov ecx, eax
mov edx, 2
mov ebx, 1
mov eax, 4
int 80h









share|improve this question













Below is my code on NASM. I am taking two integers as user input (2 and 3) and want to add them then print to stdout. Input is taking correctly but as output it is showing some arbitrary character. What I am doing wrong? I believe something is wrong in adding input block but not sure what it is.



section .bss
fnum rest 255
lnum rest 255

section .text
global _start
_start:
; first input
mov ecx, fnum
mov edx, 255
mov ebx, 0
mov eax, 3
int 80h

; second input
mov ecx, lnum
mov edx, 255
mov ebx, 0
mov eax, 3
int 80h

; adding inputs 2 and 3
mov eax, fnum
add eax, ecx
add eax, 48
push eax
mov eax, esp

;print output
mov ecx, eax
mov edx, 2
mov ebx, 1
mov eax, 4
int 80h






assembly nasm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 19:25









al mamunal mamun

33




33




migrated from unix.stackexchange.com Nov 19 '18 at 19:52


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.






migrated from unix.stackexchange.com Nov 19 '18 at 19:52


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.














  • The same arbitrary character every time, or different every time?
    – Kusalananda
    Nov 19 '18 at 19:35










  • same. something like this ▒, For simplicity I am taking inputs that will produce output less than 9.
    – al mamun
    Nov 19 '18 at 19:41






  • 1




    you'll need to reinvent printf("%dn", ...) to turn the contents of a register into a human-readable number, either by compiling in libc and calling printf or by doing it manually via something like github.com/thrig/scripts/blob/master/asm/Darwin/x86_64/…
    – thrig
    Nov 19 '18 at 19:51






  • 1




    You're printing the low 2 bytes of the sum of the addresses, not the sum of the ASCII characters at the pointed-to locations.
    – Peter Cordes
    Nov 19 '18 at 19:57












  • @thrig I think the adding 48 accomplishes that, but I don't see the equivalent subtracting 48 from the inputs (or, more simply, subtracting 48 once and never adding it).
    – Daniel H
    Nov 19 '18 at 20:00


















  • The same arbitrary character every time, or different every time?
    – Kusalananda
    Nov 19 '18 at 19:35










  • same. something like this ▒, For simplicity I am taking inputs that will produce output less than 9.
    – al mamun
    Nov 19 '18 at 19:41






  • 1




    you'll need to reinvent printf("%dn", ...) to turn the contents of a register into a human-readable number, either by compiling in libc and calling printf or by doing it manually via something like github.com/thrig/scripts/blob/master/asm/Darwin/x86_64/…
    – thrig
    Nov 19 '18 at 19:51






  • 1




    You're printing the low 2 bytes of the sum of the addresses, not the sum of the ASCII characters at the pointed-to locations.
    – Peter Cordes
    Nov 19 '18 at 19:57












  • @thrig I think the adding 48 accomplishes that, but I don't see the equivalent subtracting 48 from the inputs (or, more simply, subtracting 48 once and never adding it).
    – Daniel H
    Nov 19 '18 at 20:00
















The same arbitrary character every time, or different every time?
– Kusalananda
Nov 19 '18 at 19:35




The same arbitrary character every time, or different every time?
– Kusalananda
Nov 19 '18 at 19:35












same. something like this ▒, For simplicity I am taking inputs that will produce output less than 9.
– al mamun
Nov 19 '18 at 19:41




same. something like this ▒, For simplicity I am taking inputs that will produce output less than 9.
– al mamun
Nov 19 '18 at 19:41




1




1




you'll need to reinvent printf("%dn", ...) to turn the contents of a register into a human-readable number, either by compiling in libc and calling printf or by doing it manually via something like github.com/thrig/scripts/blob/master/asm/Darwin/x86_64/…
– thrig
Nov 19 '18 at 19:51




you'll need to reinvent printf("%dn", ...) to turn the contents of a register into a human-readable number, either by compiling in libc and calling printf or by doing it manually via something like github.com/thrig/scripts/blob/master/asm/Darwin/x86_64/…
– thrig
Nov 19 '18 at 19:51




1




1




You're printing the low 2 bytes of the sum of the addresses, not the sum of the ASCII characters at the pointed-to locations.
– Peter Cordes
Nov 19 '18 at 19:57






You're printing the low 2 bytes of the sum of the addresses, not the sum of the ASCII characters at the pointed-to locations.
– Peter Cordes
Nov 19 '18 at 19:57














@thrig I think the adding 48 accomplishes that, but I don't see the equivalent subtracting 48 from the inputs (or, more simply, subtracting 48 once and never adding it).
– Daniel H
Nov 19 '18 at 20:00




@thrig I think the adding 48 accomplishes that, but I don't see the equivalent subtracting 48 from the inputs (or, more simply, subtracting 48 once and never adding it).
– Daniel H
Nov 19 '18 at 20:00












1 Answer
1






active

oldest

votes


















0














When you read in the inputs, you ask for at least 255 bytes of text to be put into the locations fnum and lnum. You never read that text, and instead add the two memory addresses together (just the addresses, not the contents), and then add 48. You then print out the first two bytes of the result of that addition, which isn't actually what you want and isn't even meaningful.



You need to read the bytes read from those memory addresses ([fnum] instead of fnum), turn them into integers you can do arithmetic on, add those together instead of the memory addresses, turn the resulting number back into text, and then print the text.



For the simplest case, where both inputs and the output are single-digit nonnegative numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), you can convert from the character to the number by subtracting 0x30 (decimal 48), and you can convert from the number to the character by adding 0x30. This is because the ASCII digits are consecutive, starting at character 0x30 being the digit 0. I believe your third line after the comment "adding inputs 2 and 3" is supposed to be this sort of conversion, but the thing you're converting isn't the output you want.



In NASM, you can write 0x30 as '0', which makes the purpose more obvious to human readers.






share|improve this answer























  • Thank you so much for the clarification. It worked like a charm. I've assigned like mov eax, [fnum] then sub eax, 48. did same for lnum and finaly during print added 48.
    – al mamun
    Nov 20 '18 at 15:34










  • As a simplification, you can just subtract 48 once and not add it back, since [fnum] + [lnum] - 48 is mathematically equal to [fnum] - 48 + [lnum] - 48 + 48. I wouldn't recommend it because that would make the code harder to read, though.
    – Daniel H
    Nov 20 '18 at 17:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381702%2farithmetic-operation-in-nasm-showing-arbitrary-character%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














When you read in the inputs, you ask for at least 255 bytes of text to be put into the locations fnum and lnum. You never read that text, and instead add the two memory addresses together (just the addresses, not the contents), and then add 48. You then print out the first two bytes of the result of that addition, which isn't actually what you want and isn't even meaningful.



You need to read the bytes read from those memory addresses ([fnum] instead of fnum), turn them into integers you can do arithmetic on, add those together instead of the memory addresses, turn the resulting number back into text, and then print the text.



For the simplest case, where both inputs and the output are single-digit nonnegative numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), you can convert from the character to the number by subtracting 0x30 (decimal 48), and you can convert from the number to the character by adding 0x30. This is because the ASCII digits are consecutive, starting at character 0x30 being the digit 0. I believe your third line after the comment "adding inputs 2 and 3" is supposed to be this sort of conversion, but the thing you're converting isn't the output you want.



In NASM, you can write 0x30 as '0', which makes the purpose more obvious to human readers.






share|improve this answer























  • Thank you so much for the clarification. It worked like a charm. I've assigned like mov eax, [fnum] then sub eax, 48. did same for lnum and finaly during print added 48.
    – al mamun
    Nov 20 '18 at 15:34










  • As a simplification, you can just subtract 48 once and not add it back, since [fnum] + [lnum] - 48 is mathematically equal to [fnum] - 48 + [lnum] - 48 + 48. I wouldn't recommend it because that would make the code harder to read, though.
    – Daniel H
    Nov 20 '18 at 17:49
















0














When you read in the inputs, you ask for at least 255 bytes of text to be put into the locations fnum and lnum. You never read that text, and instead add the two memory addresses together (just the addresses, not the contents), and then add 48. You then print out the first two bytes of the result of that addition, which isn't actually what you want and isn't even meaningful.



You need to read the bytes read from those memory addresses ([fnum] instead of fnum), turn them into integers you can do arithmetic on, add those together instead of the memory addresses, turn the resulting number back into text, and then print the text.



For the simplest case, where both inputs and the output are single-digit nonnegative numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), you can convert from the character to the number by subtracting 0x30 (decimal 48), and you can convert from the number to the character by adding 0x30. This is because the ASCII digits are consecutive, starting at character 0x30 being the digit 0. I believe your third line after the comment "adding inputs 2 and 3" is supposed to be this sort of conversion, but the thing you're converting isn't the output you want.



In NASM, you can write 0x30 as '0', which makes the purpose more obvious to human readers.






share|improve this answer























  • Thank you so much for the clarification. It worked like a charm. I've assigned like mov eax, [fnum] then sub eax, 48. did same for lnum and finaly during print added 48.
    – al mamun
    Nov 20 '18 at 15:34










  • As a simplification, you can just subtract 48 once and not add it back, since [fnum] + [lnum] - 48 is mathematically equal to [fnum] - 48 + [lnum] - 48 + 48. I wouldn't recommend it because that would make the code harder to read, though.
    – Daniel H
    Nov 20 '18 at 17:49














0












0








0






When you read in the inputs, you ask for at least 255 bytes of text to be put into the locations fnum and lnum. You never read that text, and instead add the two memory addresses together (just the addresses, not the contents), and then add 48. You then print out the first two bytes of the result of that addition, which isn't actually what you want and isn't even meaningful.



You need to read the bytes read from those memory addresses ([fnum] instead of fnum), turn them into integers you can do arithmetic on, add those together instead of the memory addresses, turn the resulting number back into text, and then print the text.



For the simplest case, where both inputs and the output are single-digit nonnegative numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), you can convert from the character to the number by subtracting 0x30 (decimal 48), and you can convert from the number to the character by adding 0x30. This is because the ASCII digits are consecutive, starting at character 0x30 being the digit 0. I believe your third line after the comment "adding inputs 2 and 3" is supposed to be this sort of conversion, but the thing you're converting isn't the output you want.



In NASM, you can write 0x30 as '0', which makes the purpose more obvious to human readers.






share|improve this answer














When you read in the inputs, you ask for at least 255 bytes of text to be put into the locations fnum and lnum. You never read that text, and instead add the two memory addresses together (just the addresses, not the contents), and then add 48. You then print out the first two bytes of the result of that addition, which isn't actually what you want and isn't even meaningful.



You need to read the bytes read from those memory addresses ([fnum] instead of fnum), turn them into integers you can do arithmetic on, add those together instead of the memory addresses, turn the resulting number back into text, and then print the text.



For the simplest case, where both inputs and the output are single-digit nonnegative numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), you can convert from the character to the number by subtracting 0x30 (decimal 48), and you can convert from the number to the character by adding 0x30. This is because the ASCII digits are consecutive, starting at character 0x30 being the digit 0. I believe your third line after the comment "adding inputs 2 and 3" is supposed to be this sort of conversion, but the thing you're converting isn't the output you want.



In NASM, you can write 0x30 as '0', which makes the purpose more obvious to human readers.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 1:58









Peter Cordes

120k16181311




120k16181311










answered Nov 20 '18 at 1:51









Daniel HDaniel H

5,07211830




5,07211830












  • Thank you so much for the clarification. It worked like a charm. I've assigned like mov eax, [fnum] then sub eax, 48. did same for lnum and finaly during print added 48.
    – al mamun
    Nov 20 '18 at 15:34










  • As a simplification, you can just subtract 48 once and not add it back, since [fnum] + [lnum] - 48 is mathematically equal to [fnum] - 48 + [lnum] - 48 + 48. I wouldn't recommend it because that would make the code harder to read, though.
    – Daniel H
    Nov 20 '18 at 17:49


















  • Thank you so much for the clarification. It worked like a charm. I've assigned like mov eax, [fnum] then sub eax, 48. did same for lnum and finaly during print added 48.
    – al mamun
    Nov 20 '18 at 15:34










  • As a simplification, you can just subtract 48 once and not add it back, since [fnum] + [lnum] - 48 is mathematically equal to [fnum] - 48 + [lnum] - 48 + 48. I wouldn't recommend it because that would make the code harder to read, though.
    – Daniel H
    Nov 20 '18 at 17:49
















Thank you so much for the clarification. It worked like a charm. I've assigned like mov eax, [fnum] then sub eax, 48. did same for lnum and finaly during print added 48.
– al mamun
Nov 20 '18 at 15:34




Thank you so much for the clarification. It worked like a charm. I've assigned like mov eax, [fnum] then sub eax, 48. did same for lnum and finaly during print added 48.
– al mamun
Nov 20 '18 at 15:34












As a simplification, you can just subtract 48 once and not add it back, since [fnum] + [lnum] - 48 is mathematically equal to [fnum] - 48 + [lnum] - 48 + 48. I wouldn't recommend it because that would make the code harder to read, though.
– Daniel H
Nov 20 '18 at 17:49




As a simplification, you can just subtract 48 once and not add it back, since [fnum] + [lnum] - 48 is mathematically equal to [fnum] - 48 + [lnum] - 48 + 48. I wouldn't recommend it because that would make the code harder to read, though.
– Daniel H
Nov 20 '18 at 17:49


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381702%2farithmetic-operation-in-nasm-showing-arbitrary-character%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith