I
I
Im p3l2018-02-25 13:44:04
assembler
Im p3l, 2018-02-25 13:44:04

How to form a new array from an array of numbers, which should include odd numbers?

Given an array of numbers X = 0.3, -15.19, -26.49, -61.18.55.4. It is necessary to form a new array Y, which should include odd numbers.
I did it following the example of a similar program, but something is wrong and I can’t understand where.

SDATA SEGMENT WORD PUBLIC 'DATA'
 
    ARRAY_X     DB 0,3,-15,19,-26,49,-61,18,55,4 ; - массив чисел X
    ARRAY_Y     DB 10 DUP(0)                        ; - массив чисел Y
 
SDATA ENDS
 
SSTACK SEGMENT PARA STACK 'STACK'
 
    DW 64 DUP(?)
        
STACK_POINT LABEL WORD
SSTACK ENDS
 
START: MOV AX,SDATA             
    MOV DS,AX                   
    MOV AX,SSTACK       
    MOV SS,AX               
    MOV SP,OFFSET STACK_POINT   
    MOV SI,0                
    MOV DI,0                
    MOV CX,10           
                            
M1: MOV AL,ARRAY_X[SI] 
    IDIV 2  
    CMP AL,2            
    JGE M2                  
    MOV ARRAY_Y[DI],AL      
    INC DI                  
        
M2: INC SI                  
    LOOP M1                 
    MOV AX,4C00H            
    INT 21H                 
                            
SCODE   ENDS                
END START

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2018-02-25
@LoranDeMarcus

IDIV 2  
    CMP AL,2            
    JGE M2

This code jumps to M2 if AL >= 2, but: 1) idiv puts the remainder of division into AH, and 2) the remainder of division by 2 is either 0 or 1.
In general, instead of dividing, one could simply check the value of the zero bit:
M1: MOV AL,ARRAY_X[SI] 
    TEST AL, 1
    JZ M2
    MOV ARRAY_Y[DI],AL      
    INC DI                  
        
M2: INC SI                  
    LOOP M1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question