mimimi
mimimi

Reputation: 11

Reading input and calculating parity in assembly

A decimal number of N digits ending with $ is read from the keyboard. (N<10). Print the number of digits of the number and its mathematical parity in an emu8086 program (ASM).

I can't seem to understand what I am doing wrong. Is "input" an invalid instruction? How can i read a number from the keyboard in assembly?

numCifre db 0

start:
    input number

    mov cx, 0
    .numaraCifre:
        mov ax, number
        xor dx, dx
        div byte 10
        inc cx
        cmp ax, 0
        jne .numaraCifre

    mov dl, cl
    add dl, '0'
    mov ah, 0Ah
    int 21h

    mov ax, number
    and ax, 1
    jz .par
    mov dl, 'I'
    jmp .showParity
    .par:
        mov dl, 'P'
    .showParity:
        mov ah, 0Ah
        int 21h

    mov ax, 4C00h
    int 21h

Upvotes: 1

Views: 94

Answers (1)

Sep Roland
Sep Roland

Reputation: 39206

Is "input" (input number) an invalid instruction?

It is certainly not an instruction. It could possibly be a macro, if one were defined so...

How can i read a number from the keyboard in assembly?

Inputting multi-radix multi-digit signed numbers with DOS explains this and even has a DeLuxe version on offer.
However, for your particular task

A decimal number of N digits ending with $ is read from the keyboard. (N<10).

it will be a simple matter of inputting single digits (up to 9 of them):

  xor  cx, cx
Again:
  mov  ah, 01h    ; DOS.GetKeystroke
  int  21h        ; -> AL
  cmp  al, '$'
  je   Done
  cmp  al, '0'
  jb   Again      ; Not a digit
  cmp  al, '9'
  ja   Again      ; Not a digit
  mov  bl, al     ; Remember the last (least significant) digit
  inc  cx
  cmp  cx, 9
  jb   Again
Done:

Print the number of digits of the number and its mathematical parity in an emu8086 program (ASM).

Because the number of digits is between 0 and 9, printing the value is almost like you did it except that you were using the wrong function number 0Ah.

  mov  dl, cl     ; [0,9]
  add  dl, '0'    ; -> ["0","9"]
  mov  ah, 02h    ; DOS.PrintCharacter
  int  21h

For the mathematical parity (aka even/odd), you only need to consider the least significant digit of the number. That's why in the above input routine I stored the last digit in the BL register.

  mov  dl, 'P'
  shr  bl, 1      ; Inspecting the lowest bit of the last digit
  jnc  IsEven
  mov  dl, 'I'
IsEven:
  mov  ah, 02h    ; DOS.PrintCharacter
  int  21h

In your .numaraCifre div byte 10 is wrong. An immediate operand is not possible, and even if 10 were referring to a memory variable (with emu8086 it wouldn't surprise me) it would still have to be a word instead of a byte.

Upvotes: 2

Related Questions