Page 1 of 1

{ASM} decrementing a no. to 1

Posted: Tue Mar 21, 2006 1:50 pm
by rich_m
i'm supposed to make this program decrement a given number till it reaches '1'.

This code doesnt seem to work

Code: Select all

.model small
.stack
.data
  n1 db "S=","$"
  t db ?
  result db " -1 = ","$"
.code
main proc
       mov ax,seg n1         ;Printing n1
       mov ds,ax
       lea dx,n1
       mov ah,09h
       int 21h

       mov ah,01h            ;Get first value
       int 21h

       mov [t],al


    loo:

       cmp [t],01h
       je quit

       mov dl,[t]             ;Display t
       mov ah,02h
       int 21h

       mov ax,seg result      ;Print the string called result
       mov ds,ax
       lea dx,result
       mov ah,09h
       int 21h

       dec [t]
       jne loo

quit:
       mov ax,4c00h          ;Quit the execution
       int 21h
       main endp
       END main
p.s m using TASM

Re:{ASM} decrementing a no. to 1

Posted: Tue Mar 21, 2006 3:03 pm
by DennisCGc
Hi,

First off, the ascii equilevant of 1 is not 1, but 49. DOS only returns ASCII values so a 5 will return 54. For more information, please refer to the wikipedia page about ASCII. In short, you're comparing it with the wrong value, it should be 49

Further more, you don't have to reload the DS segment everytime. IIRC under DOS this is already set by DOS, so you do not have to mess with that.

Further it looks okay, but I have not that much experience with DOS programming (with ASM), have more experience with OS writing and Win32asm.

Gl,

DennisCGc.

Re:{ASM} decrementing a no. to 1

Posted: Tue Mar 21, 2006 3:05 pm
by Candy
DennisCGc wrote: First off, the ascii equilevant of 1 is not 1, but 49. DOS only returns ASCII values so a 5 will return 54.
1+4 = 5
49 + 4 != 54

Re:{ASM} decrementing a no. to 1

Posted: Tue Mar 21, 2006 3:23 pm
by DennisCGc
Candy wrote: 1+4 = 5
49 + 4 != 54
Oops :-[ And not to mention I have a math test tomorrow.. (I'm screwed :P )

Re:{ASM} decrementing a no. to 1

Posted: Wed Mar 22, 2006 10:49 am
by rich_m
ok thnaks, got that working. Can anyone expalain how the "MUL" directive works, i'm trying to modify that program to a find factorial values.

Re:{ASM} decrementing a no. to 1

Posted: Wed Mar 22, 2006 11:24 am
by Candy
rich_m wrote: ok thnaks, got that working. Can anyone expalain how the "MUL" directive works, i'm trying to modify that program to a find factorial values.
mul : edx:eax = eax * <32-bit value>
or dx:ax = ax * <16-bit value>
or ax = al * <8-bit value>

all unsigned iirc. You specify which value with the one parameter.