LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-01-2011, 11:45 AM   #1
displaytor
LQ Newbie
 
Registered: Dec 2005
Posts: 2

Rep: Reputation: 0
Beginner NASM arithmetic & unwanted output/result


What I'm trying to do is ask the user to input a number, then keep adding every number from the defined one until the counter reaches zero and finally print the sum on the screen. Also, I only want to use system calls (no C libs).

example output:
Add from 1 to X - give X <-program askas
10 <-this I inserted
10 <-this is the output, no go

The code is here
http://pastebin.com/BrVUEHdS

Thanks in advance, any help is highly appreciated.
 
Old 02-01-2011, 03:17 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,139

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by displaytor View Post
What I'm trying to do is ask the user to input a number, then keep adding every number from the defined one until the counter reaches zero and finally print the sum on the screen. Also, I only want to use system calls (no C libs).
Are you asking for someone to write that program for you, or are you trying to learn how to do that yourself?

If the code you posted had been close to correct, I wouldn't need to ask that. But your code is far enough from correct that fixing it is nearly writing it from scratch.

I think you should give the problem more thought:

Your current input code:
Code:
;user input
        mov eax,3             ; syscall: read
        mov ebx,0             ; keyboard
        mov ecx,choice           ; offset
        int 80h              ; kernel call
Think about what an input operation is: A sequence of characters is read into a buffer. How long is the buffer? How does the syscall know how long the buffer is (to avoid giving you too much input)? How does the program know how much input it got (might be less than a full buffer)? Is the terminating newline character part of the text delivered to the buffer?

Once you have the input, what do you have? If the user pressed '1' then '0' then newline, what translates all that into the number ten that you seem to want?

Your processing code:
Code:
;arithmetic
calc:
        add edx,ecx
        dec ecx
        jecxz result
What was in edx before that code was reached? What was in ecx before that code was reached? What happens after jecxz if ecx has reached zero? What happens if ecx hasn't reached zero?

The above issues are not nearly the only problems. Look at every section of your code that might not be working and ask yourself similar questions.

Last edited by johnsfine; 02-01-2011 at 03:24 PM.
 
  


Reply

Tags
asm, assembly, nasm


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unwanted dd result pofadda Linux - General 5 02-21-2010 07:23 PM
m4 sendmail.mc > sendmail.cf generate unwanted result on OpenSolaris johncsl82 Solaris / OpenSolaris 1 09-09-2008 12:25 AM
Find command, eliminating unwanted output swamprat Linux - Newbie 7 04-13-2008 07:28 PM
nasm & linker rblampain Programming 2 05-14-2007 06:23 AM
nasm assembler; output numbers? infinity42 Programming 3 11-15-2005 02:02 PM


All times are GMT -5. The time now is 01:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration