{ASM} decrementing a no. to 1

Programming, for all ages and all languages.
Post Reply
rich_m

{ASM} decrementing a no. to 1

Post 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
DennisCGc

Re:{ASM} decrementing a no. to 1

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:{ASM} decrementing a no. to 1

Post 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
DennisCGc

Re:{ASM} decrementing a no. to 1

Post 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 )
rich_m

Re:{ASM} decrementing a no. to 1

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:{ASM} decrementing a no. to 1

Post 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.
Post Reply