SARA
SARA

Reputation: 1

Assembly code elements of array

I want to write an assembly code, for the following commands:array of 4 elements, read the elements from the user and store them, display the array with a proper message and multiply each element by 2 and I have some problems, first of all:
I wrote this code:

.MODEL SMALL
.STACK 100H
.DATA
ARR DB  1,2,3,4,4,4,3,1,2
MSG1    DB  'The elements of an array : $'
MSG2    DB 0DH, 0AH,'The value after multiply by 2: 

$'
.CODE
MAIN PROC

    MOV AX, @DATA
    MOV DS, AX

    MOV CX, 9
    MOV BX, 0

    MOV AH,9
    LEA DX, MSG1
    INT 21H

DISPLAY1:
    MOV AL, ARR[BX]
    MOV AH, 2
    ADD AL, 30H
    MOV DL, AL
    INT 21H
    INC BX
LOOP DISPLAY1

    MOV AH,9
    LEA DX, MSG2
    INT 21H

    XOR CX, CX
    XOR BX, BX
    MOV CX, 9
    MOV BX, 0
MULTIPLAY:
MOV AL, 2
MUL ARR[BX]
MOV AH, 2
ADD AL, 30H
MOV DL, AL
INT 21H
INC BX
LOOP MULTIPLAY

MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN


Output:

The elements of an array : 123444312
The value after multiply by 2:246888624

It running well, but when I'm trying to ask the user to enter the number the result isn't given number in decimal digit (that gives form ascii code!)

.MODEL SMALL
.STACK 100H
.DATA
ARR DB      4 DUP (?)
MSG1    DB 0DH, 0AH,    'The elements of an array : $'
MSG2    DB 0DH, 0AH,    'The value after multiply by 2: $'
MSG3    DB 0DH, 0AH,    'Enter 4 elements (numbers) : $'
.CODE
MAIN PROC

    MOV AX, @DATA
    MOV DS, AX

    MOV CX, 4
    MOV BX, 0

    MOV AH,9
    LEA DX, MSG3
    INT 21H
INPUT:          ;This for the input elements of an array
    MOV AL, ARR[BX]
    MOV AH, 1
    SUB AL, 30H     ; I do this to convert from ASCII
    MOV DL, AL
    INT 21H
    INC BX
LOOP INPUT

    MOV AH,9
    LEA DX, MSG1
    INT 21H

    XOR CX, CX
    XOR BX, BX
    MOV CX, 4
    MOV BX, 0

DISPLAY1:       ;Output elements of an array
    MOV AL, ARR[BX]
    MOV AH, 2   
    ADD AL, 30H ; I do this to convert from ASCII
    MOV DL, AL
    INT 21H
    INC BX
LOOP DISPLAY1

    MOV AH,9
    LEA DX, MSG2
    INT 21H

    XOR CX, CX
    XOR BX, BX
    MOV CX, 4
    MOV BX, 0
MULTIPLAY:

    MOV AL, 2
    MUL ARR[BX]
    MOV AH, 2
    ADD AL, 30H  ; I do this to convert from ASCII
    MOV DL, AL
    INT 21H
    INC BX
LOOP MULTIPLAY

MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

The problem in : output, display1 and multiplay functions.
Output:

Enter 4 elements (numbers) : 1234
The elements of an array : 0000
The value after multiplay by 2: 0000

How can I correct them?

Upvotes: 0

Views: 1850

Answers (1)

Jester
Jester

Reputation: 58762

You have the input block messed up, you use the character before you even read it, and also you read from the array instead of writing. Something like this should work better:

INPUT:              ;This for the input elements of an array
    MOV AH, 1       ; read char with echo function
    INT 21H         ; character returned in AL
    SUB AL, 30H     ; convert from ASCII
    MOV ARR[BX], AL ; store into array
    INC BX
    LOOP INPUT

PS: learn to use a debugger so you can find your own mistakes.

Upvotes: 2

Related Questions