LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 02-01-2011, 10: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, 02:17 PM   #2
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Centos
Posts: 4,969

Rep: Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075
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 02: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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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


All times are GMT -5. The time now is 01:13 AM.

Main Menu
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