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$"