NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: avajos on April 16, 2014, 10:19:15 AM

Title: displaying output 50 times instead of 3
Post by: avajos on April 16, 2014, 10:19:15 AM
when input accepted for temp is 3...it prints the message 50 times..!!!..
Why does it happen so..??

thanks in advance,
avajos.
Title: Re: displaying output 50 times instead of 3
Post by: encryptor256 on April 16, 2014, 02:46:27 PM
Quote
when input accepted for temp is 3...it prints the message 50 times..!!!..

Actually 51 times.

Wonder why? I know why!

ASCII character "3" is equals to number 51.

Title: Re: displaying output 50 times instead of 3
Post by: z80a on April 19, 2014, 03:25:56 AM
Input will be in ASCII characters, you need to mask to binary. ASCII 0 - 9 is hexidecimal 30h - 39h, or binary 00110000 - 00111001
By masking off upper 4 bits you get binary equivalent of that digit. Alpha letters can be converted upper/lower case with bit masks
also. To see how its working, I recommend Evans debugger (codef00.com) edb to step through program. This program is written for
Linux 64 bit.

Quote
; Comments:  Program is to show concept, no error checking. Input
;            is possible to overrun char buffer and/or enter char's
;            other than numeric.
;            ASCII char's 0-9 equal hex 30h - 39h
;            binary 0011 0111 = ASCII '7'. Mask off upper 4 bits to
;            change to binary 7.
; To compile:
;      nasm -f elf64 april.asm
;      ld -melf_x86_64 -o april april.o
;      edb --run "/path to program"/april
; NOTE: Recommend Evans debugger (codef00.com) edb to observe execution.
;=======================================================================
;
      Bits 64
      Section      .data
msg      db   'Enter a number: '
msglen   equ   $ - msg

      Section      .bss
cbuffer   resb   32

      Section      .text
      Global      _start

_start:      mov      rax,1      ; (write)
      mov      rdi,1
      mov      rsi,msg
      mov      rdx,msglen
      syscall
      mov      rax,0      ; (read)
      mov      rdi,0
      mov      rsi,cbuffer
      mov      rdx,10h
      syscall
;
      xor      rax,rax      ;Result register
      xor      rbx,rbx      ;Temp ASCII digit
      mov      rcx,0Ah      ;x 10 multiplier
      mov      rsi,cbuffer   ;Where ASCII string
_cnvrt:      mov      bl,[rsi]   ;Get numeric char
      cmp      bl,0Ah      ;Linefeed?
      jz      _done      ; we're done
      mul      rcx      ;Multiply by 10
      and      bl,0Fh      ;Mask ASCII to binary
      add      rax,rbx      ;Add to result
      inc      rsi      ;Increment string pointer
      jmp      _cnvrt      ;Next character
;
; At this point RAX contains binary conversion.
;
_done:      mov      rax,3Ch      ; (exit)
      mov      rdi,0
      syscall