Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Displaying a character

[b][red]This message was edited by stevem222 at 2002-9-24 11:18:37[/red][/b][hr]
I know that I am missing something, but I don't know what. Can somebody help me? I want to be able to be prompted in dos for the number (which I am), input the number (can do that too), and have that number returned (not happening; get an upward arrow). See below:

;;program displaying a number 1-8

.model small
.stack 100h
.data
Prompt db 'Enter a number 1-8: $'
msg db 10, 13, '$'
msg2 db 'Factorial is: $'

.code
fact proc
mov ax, @data
mov ds,ax
mov dx,offset prompt
mov ah,9h
int 21h

mov ah, 01h; get character input
int 21h


mov ah,9h
mov dx, offset msg
int 21h

mov ah,9h
mov dx, offset msg2
int 21h

mov ah, 02h
int 21h

mov ah,4ch;send return to dos code
int 21h

fact endp
end fact



Comments

  • jeffleydajeffleyda Posts: 390Member
    : [b][red]This message was edited by stevem222 at 2002-9-24 11:18:37[/red][/b][hr]
    : I know that I am missing something, but I don't know what. Can somebody help me? I want to be able to be prompted in dos for the number (which I am), input the number (can do that too), and have that number returned (not happening; get an upward arrow). See below:


    The int 21h, function 02 routine (print character) uses whatever value (character) is in DL.

    After you input your number value, you never do anything with it.
    Try moving it into DL and see what happens. Make sure you move it into DL at the right spot in yr code though, otherwise you'll overwrite it.






  • stevem222stevem222 Posts: 24Member
    : : [b][red]This message was edited by stevem222 at 2002-9-24 11:18:37[/red][/b][hr]
    : : I know that I am missing something, but I don't know what. Can somebody help me? I want to be able to be prompted in dos for the number (which I am), input the number (can do that too), and have that number returned (not happening; get an upward arrow). See below:
    :
    :
    : The int 21h, function 02 routine (print character) uses whatever value (character) is in DL.
    :
    : After you input your number value, you never do anything with it.
    : Try moving it into DL and see what happens. Make sure you move it into DL at the right spot in yr code though, otherwise you'll overwrite it.
    :
    : Yes, after a little thought, I found that I was putting a bunch of stuff on a register, without clearing first as well. I took care of this, and am now going to use this for a recursive program to find the factorial of an entered number. We'll see how it goes.
    :
    :
    :
    :
    :

  • stevem222stevem222 Posts: 24Member
    For the practice run, just displaying the input character, here is what I got.
    ;;program displaying a number 1-8

    .model small
    .stack 100h
    .data
    Prompt db 'Enter a number 1-8: $'
    msg2 db 10, 10, 13, 'Factorial is: $'

    .code
    fact proc
    mov ax, @data
    mov ds,ax
    mov dx,offset prompt
    mov ah,9h
    int 21h

    mov ah, 01h; get character input
    int 21h
    mov ah, 0
    push ax

    mov ah,9h
    mov dx, offset msg2
    int 21h

    pop dx
    mov ah, 02h
    int 21h

    mov ah,4ch;send return to dos code
    int 21h

    fact endp
    end fact

    From that, I will work with the same program, creating what you see below, but I will need to verify, and write a recursive portion to display the factorial of an entered number. See what I will be working with below:

    ;;program displaying a number 1-8

    .model small
    .stack 100h
    .data
    Prompt db 'Enter a number 1-8: $'
    msg2 db 10, 10, 13, 'Factorial is: $'

    .code
    fact proc
    mov ax, @data
    mov ds,ax
    mov dx,offset prompt
    mov ah,9h
    int 21h

    mov ah, 01h; get character input
    int 21h


    mov ah, 0
    sub ax, 30h; make it a number from ASCII char
    push ax

    call div10; proc to create the base 10

    mov ah,9h
    mov dx, offset msg2
    int 21h

    mov ah,4ch;send return to dos code
    int 21h

    fact endp
    div10 proc
    mov cx,0

    divi10:

    mov dx, 0
    mov bx, 10
    div bx
    push dx
    add cx, 1
    cmp ax, 0
    jg divi10

    disp10:

    pop dx
    add dl, 30h
    mov ah, 02h
    int 21h
    loop disp10

    ret; return after the call

    div10 endp
    end fact

  • emu8086emu8086 Posts: 125Member
    You may be interested in this sample code that calculates
    the factorial for any number between 0 and 8:

    This source is optimized for emu8086; but you may easily
    convert it to MASM/TASM syntax:
    http://www.geocities.com/emu8086/Help/compatibility.html


    ; This sample gets the number
    ; from the user, and calculates
    ; factorial for it.
    ; Supported input from 0 to 8
    ; inclusive!

    #make_COM#

    include 'emu8086.inc'

    ORG 100h

    start:

    ; get first number:

    CALL PTHIS
    DB 13, 10, 'Enter the number: ', 0

    CALL scan_num


    ; factorial of 0 = 1:
    MOV AX, 1
    CMP CX, 0
    JE print_result

    ; move the number to BX:
    ; CX will be a counter:

    MOV BX, CX

    MOV AX, 1
    MOV BX, 1

    calc:
    MUL BX
    CMP DX, 0
    JNE overflow
    INC BX
    LOOP calc

    print_result:

    ; print result in AX:
    CALL PTHIS
    DB 13, 10, 'Factorial: ', 0

    CALL PRINT_NUM_UNS
    JMP exit


    overflow:
    CALL PTHIS
    DB 13, 10, 'The result is too big!', 13, 10, 'Use values from 0 to 8.', 0
    JMP start

    exit:

    RET

    ;=================================
    ; here we define the functions
    ; from emu8086.inc

    ; SCAN_NUM reads a
    ; number from the user and stores
    ; it in CX register.

    DEFINE_SCAN_NUM

    ; PRINT_NUM prints a signed
    ; number in AX.
    ; PRINT_NUM_UNS prints an unsigned
    ; number in AX (required by
    ; PRINT_NUM):

    DEFINE_PRINT_NUM
    DEFINE_PRINT_NUM_UNS

    ; PTHIS prints NULL terminated
    ; string defined just after
    ; the CALL PTHIS instruction:
    DEFINE_PTHIS

    ;=================================
    END

  • stevem222stevem222 Posts: 24Member
    I'd have to try that out in my "free time." We are not only discouraged, but prohibited to use utilities. Macro is a no no in this class. But, to keep myself fresh, I will practice everything. Thanks!
  • stevem222stevem222 Posts: 24Member
    here was the result of my program. Check it out if you'd like:

    ;;program displaying the factorial of a number 1-8
    ;;Enter a number between 1 and 8
    ;;Validates that the number is between 1 and 8
    ;;subtracts 30 hex from the ASCII Character
    ;;calls factorial proc, N(N-1)...
    ;;calls process to divide by 10
    ;;displays factorial of inputted number


    .model small
    .stack 100h
    .data
    Prompt db 10, 13, 'Enter a number 1-8: $'
    msg2 db 10, 10, 13, 'Factorial is: $'

    .code
    fact proc
    mov ax, @data
    mov ds,ax
    do_over:
    mov dx,offset prompt
    mov ah,9h
    int 21h

    mov ah, 01h; get character input
    int 21h

    mov ah, 0
    sub ax, 30h; make it a number from ASCII char
    push ax

    cmp al, 1
    jb do_over; jump if below 1
    cmp al, 8
    jg do_over; jump if greater than 8

    mov ah, 9h
    mov dx, offset msg2
    int 21h

    call facto; factorial proc

    call div10; proc to create the base 10

    mov ah,4ch;send return to dos code
    int 21h

    fact endp

    facto proc
    pop bx; pop from stack
    pop ax; pop from stack
    push bx; push back onto stack

    cmp ax,1; compare ax to 1
    je lonley

    push ax; onto stack
    dec ax; simply decrease ax by 1
    push ax; onto stack, decreased by 1
    call facto; back to top of proc
    pop bx; from stack
    mul bx; multiply

    lonley:
    ret; to main next proc div10
    Facto endp

    div10 proc
    mov cx,0

    divi10:

    mov dx, 0
    mov bx, 10
    div bx
    push dx
    add cx, 1
    cmp ax, 0
    jg divi10

    disp10:

    pop dx
    add dl, 30h
    mov ah, 02h
    int 21h
    loop disp10

    ret; return after the call

    div10 endp
    end fact
  • emu8086emu8086 Posts: 125Member
    Even if macro are not allowed in your class, you can just copy & paste
    the code from "emu8086.inc" into your program. By the way, in
    case you maybe interested, here is your program converted to emu8086
    syntax:

    ; Great code by the way!

    [code]

    #make_COM#

    ; COM file is loaded at CS:0100h
    ; (the value of CS is set by Operating System)
    ORG 100h

    ;;program displaying the factorial of a number 1-8
    ;;Enter a number between 1 and 8
    ;;Validates that the number is between 1 and 8
    ;;subtracts 30 hex from the ASCII Character
    ;;calls factorial proc, N(N-1)...
    ;;calls process to divide by 10
    ;;displays factorial of inputted number


    jmp start

    Prompt db 10, 13, 'Enter a number 1-8: $'
    msg2 db 10, 10, 13, 'Factorial is: $'

    start:

    fact proc
    mov ax, cs
    mov ds,ax
    do_over:
    mov dx,offset prompt
    mov ah,9h
    int 21h

    mov ah, 01h; get character input
    int 21h

    mov ah, 0
    sub ax, 30h; make it a number from ASCII char
    push ax

    cmp al, 1
    jb do_over; jump if below 1
    cmp al, 8
    jg do_over; jump if greater than 8

    mov ah, 9h
    mov dx, offset msg2
    int 21h

    call facto; factorial proc

    call div10; proc to create the base 10

    mov ah,4ch;send return to dos code
    int 21h

    fact endp

    facto proc
    pop bx; pop from stack
    pop ax; pop from stack
    push bx; push back onto stack

    cmp ax,1; compare ax to 1
    je lonley

    push ax; onto stack
    dec ax; simply decrease ax by 1
    push ax; onto stack, decreased by 1
    call facto; back to top of proc
    pop bx; from stack
    mul bx; multiply

    lonley:
    ret; to main next proc div10
    Facto endp

    div10 proc
    mov cx,0

    divi10:

    mov dx, 0
    mov bx, 10
    div bx
    push dx
    add cx, 1
    cmp ax, 0
    jg divi10

    disp10:

    pop dx
    add dl, 30h
    mov ah, 02h
    int 21h
    loop disp10

    ret; return after the call

    div10 endp
    end fact

    [/code]
  • stevem222stevem222 Posts: 24Member
    hey, thanks. I'll check it out. The next thing I would like to do is display a different message if 0 or 9 is entered. The way it is now, it just jumps back to the prompt. I am gonna work on jumping to a different message (a new one), and then going through the process as normal. It will take some work though, since I have 3 weeks experience with this language.
Sign In or Register to comment.