Author Topic: Read an integer from a string  (Read 26260 times)

stphanef

  • Guest
Read an integer from a string
« on: October 12, 2007, 11:04:58 PM »
Hi.

This is my first complete and working assembly project (albeit very small). It assembles to a .COM file and run under DOS. I wanted a program that reads an number from a string and load it in a register or memory. So this project reads the command line, converts it to 16bits unsigned integer and then display the hexadecimal representation (the display part is only to have something to see). I'd like to receive comment and criticism about it.

I'm a bit proud of myself tonight so there it is:

org 0x100                  

section .text

start:
        xor ch, ch                     ;init counter
        cld                            ;lods go forward

;find first significative digit
        mov si, 0x81                   ;ptr to command line
check_space:
        lodsb                          ;get char from command line
        cmp al, " "                    ;it's a space?
        je check_space                 ;yes: skip it
        dec si                         ;go back one char
check_zero:
        lodsb                          ;get char from command line
        cmp al, "0"                    ;it's zero?
        je check_zero                  ;yes: skip it

;find last digit
find_last:
        cmp al, "0"                    ;is the char before "0"?
        jb decode                      ;yes: we are after the end
        cmp al, "9"                    ;is the char after "9"?
        ja decode                      ;yes: we are after the end
        inc ch                         ;count one more digit
        lodsb                          ;get a char from command line
        jmp find_last                  ;next step toward last digit

;decode the decimal number
decode:
        cmp ch, 5                      ;is there more than 5 digit?
        ja overflow                    ;yes: overflow!
        xor di, di                     ;di will receive the final value
        std                            ;lods now goes backward
        dec si                         ;adjust si
        dec si                         ;adjust si
        mov bx, 1                      ;we begin by units.
.next:
        test ch, ch                    ;no more digit?
        jz print                       ;yes: ready to display the number
        lodsb                          ;get a digit from command line
        and al, 0xf                    ;translation ascii-->binary

;add bx ax-time to di
.next2:
        test al, al                    ;done?
        jz .cont                       ;yes: continue
        add di, bx                     ;add bx to di one more time
        jc overflow                    ;if there is a carry, we overflowed
        dec al                         ;step done
        jmp .next2                     ;next step

;multiply bx by ten
.cont:
        mov ax, bx                     ;ax = bx
        shl ax, 1                      ;ax = 2*bx
        shl bx, 3                      ;bx = 8*bx
        add bx, ax                     ;bx = ax+bx

dec ch                         ;one less digit to decode
        jmp .next                      ;next digit

overflow:
        mov ah, 9
        mov dx, error_message
        int 0x21
        ret                            

print:
        mov cx, 4                      ;there is four symbols to display
        mov ah, 2                      ;dos fonction call number
.next:
        mov bx, di                     ;nibble to display into bx
        shr bx, 12                     ;isolate nibble
        mov dl, [bx+symbols]           ;translation binaire--->ascii
        shl di, 4                      ;eliminate that nibble from di
        int 0x21                       ;display the hex ascii symbols
        loop .next                     ;next nibble
        ret                            ;all done!

symbols:        db      "0123456789ABCDEF"
error_message:  db      "Overflow$"

nobody

  • Guest
Re: Read an integer from a string
« Reply #1 on: October 13, 2007, 10:28:56 PM »
HI I AM NEW TO NASM
CAN YOU SAY ME HOW GETTING START
THANKS IN THIS SAME PAGE

nobody

  • Guest
Re: Read an integer from a string
« Reply #2 on: October 13, 2007, 10:31:05 PM »
JAMES: COMMENTS ARE NEEDED: HOW TO COMPILE AND EXECUTED

nobody

  • Guest
Re: Read an integer from a string
« Reply #3 on: October 14, 2007, 11:40:20 PM »
Wow!...

First, nice job, "stphanef".

Second, it looks like a dos .com file, so the command line to Nasm would be:

nasm -f bin -o dec2hex.com dec2hex.asm

(perhaps "nasmw ...")

... assuming you named it "dec2hex.asm", that is. You might want to add "-O999" to that - it'll make it a few bytes shorter. You might want to add "-l dec2hex.lst" to get a listing file. (personally, I don't see much use for listing files, but some people swear by 'em) You might want to add "[map dec2hex.map]" - to the source code, not on the command line (there are options - RTFM). *That* I find useful. But the simple command line above will create the executable.

As to where to get started... how pale green a newbie *are* ya? Start with "hello world" - usually using int 21h, ah=9 to print a '$'-terminated string... if you're doing dos. Then I'd try displaying a string in other ways - int 21h, ah=40h - write to a file, using "stdout" - 1 - as a handle. Try displaying one character at a time. We all know "dos uses int 21h"... but int 29h is also fun and simple - prints the character in al, simple as that (unfortunately, it can't be redirected). Try displaying a number - first one digit, then multiple-digit numbers. Try displaying a number in binary, or hex...

Learn to get input from the user - from the command line given to the program, and interactively from your program. Convert an ascii string representing a number into the number.

Learn to use subroutines - how to pass parameters to 'em on the stack - how to access the parameters from your subroutine - the difference between "ret" and "ret n"...

Somewhere along the line, you'll probably want to kiss dos goodbye, and learn to do it all in Windows, or Linux, or...

That's how I'd get started. How you get started depends on where you want to go, I guess. What do you find "interesting"?

Best,
Frank