1 Replies - 3879 Views - Last Post: 03 November 2012 - 05:14 PM

#1 rileyh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-October 12

linux nasm assembly read in integer and add 1 to it

Posted 03 November 2012 - 12:27 AM

Hi everyone,

I am working through a set of tasks that I am using for practice, and one of the tasks requires you to read in an integer and add 1 to it.

For example:

Please enter a number: 4
      5


The problem that I am having is that I can't figure out how to deal with place values. For example, if I were given the input 9, I can't figure out how to make that into 10 (set it to 1 and append a 0). And then, what if I were given the input 9999999? I would have to set specifically the first digit to 1, the rest zero, and append a zero!

Here is my code so far. All it does is read in the number of up to 32 bits, and print out that number (practice printing out multiple digits):

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	



So, how can I do this problem? Could someone post code, so that I can use that code in future programs?

Many thanks in advance,

Rileyh

Is This A Good Question/Topic? 0
  • +

Replies To: linux nasm assembly read in integer and add 1 to it

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,281
  • Joined: 28-March 11

Re: linux nasm assembly read in integer and add 1 to it

Posted 03 November 2012 - 05:14 PM

First, let's move this on over to the Assembly Forum. Next, internet is still down and cell service is not 100% yet, so i can't really give you am example, but i will try by transferring to cell and uploading... give me some time...

but honestly i don't really want to just give you code, you have not made any attempts!


Quote

read in an integer and add 1 to it.
and where is your b code to do that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1