Question

I am trying to write a subprocedure that will count the number of buts set in a 16bit number, then send that number (bits set) back to the main procedure in AX. The main should display display the number of 1s and determine if the number is even or odd.

I am trying to count the 1s by shifting left and incrementing on carry. The problem seems to be that when I get back to 'main,' the original input number is still in AX rather than the count I got in 'parity.' I don't know why it isn't changing.

    ;===================================================================
    ;                   MAIN.ASM
    ;===================================================================
         EXTERN  GETDEC$:FAR
         EXTERN  NEWLINE:FAR
         EXTERN  PUTSTRNG:FAR
         EXTERN  PUTDEC$:FAR
         EXTERN  PUTBIN:FAR
         EXTERN  PARITY:FAR
    ;===================================================================
        .MODEL  LARGE
        .STACK  512
    ;===================================================================
    ; D A T A   S E G M E N T   D E F I N I T I O N
        .DATA
    NUMBER      DW  ?
    PROMPT      DB 'Enter a number: '
    BINDISPLAY      DB 'Number in binary: '
    ONESDISPLAY     DB 'Number of 1s: '
    ODDDISPLAY      DB 'The number of 1s is odd. '
    EVENDISPLAY DB 'The number of 1s is even. '  
    ;===================================================================
    ; C O D E   S E G M E N T   D E F I N I T I O N
        .CODE
            ASSUME  DS:DGROUP
    ;===================================================================

    MAIN    PROC

        MOV     AX,DGROUP           ;SET DS-REGISTER TO POINT TO
        MOV     DS,AX               ;DATA SEGMENT
        MOV     ES,AX               ;AND ES ALSO

        CALL    NEWLINE
        MOV     DI, OFFSET PROMPT
        MOV     CX, SIZEOF PROMPT
        CALL    PUTSTRNG
        CALL    GETDEC$
        CALL    NEWLINE
        MOV     DI, OFFSET BINDISPLAY
        MOV     CX, SIZEOF BINDISPLAY
        CALL    PUTSTRNG 
    CALL    PUTBIN
        PUSH    AX
        CALL    PARITY 
    CALL    NEWLINE


        CALL    NEWLINE
        MOV     DI, OFFSET ONESDISPLAY
        MOV     CX, SIZEOF ONESDISPLAY
        CALL    PUTSTRNG
    POP AX
        CALL    PUTDEC$
        CALL    NEWLINE

    SUB DX, DX
        MOV     BX, 2
        DIV     BX
        CMP     DX, 0
        JNE     ODDS

        MOV     DI, OFFSET EVENDISPLAY
        MOV     CX, SIZEOF EVENDISPLAY
        CALL    NEWLINE
        CALL    PUTSTRNG
        JMP     EXIT_PROGRAM
ODDS:    
        MOV     DI, OFFSET ODDDISPLAY
        MOV     CX, SIZEOF ODDDISPLAY
        CALL    NEWLINE
        CALL    PUTSTRNG       
EXIT_PROGRAM:
    .EXIT
        MOV     AX, 4C00H
        INT     21H
MAIN    ENDP
        END MAIN

;;===================================================================
;              Veronica Kaufman
;                 CISP 310
;                PARITY.ASM
;===================================================================
         .MODEL  LARGE     
;===================================================================
; D A T A   S E G M E N T   D E F I N I T I O N
        .DATA 
ONES_COUNT     DW      0                           
;===================================================================
; C O D E   S E G M E N T   D E F I N I T I O N
        .CODE   
        ASSUME  DS:DGROUP
;===================================================================

PARITY  PROC    FAR PUBLIC USES CX DX DS
    POP AX
NUM_LOOP:
    CMP AX, 0
    JE  END_PROGRAM
    SHL AX, 1
    JC  INCREMENT
    JMP NUM_LOOP
INCREMENT:
    INC ONES_COUNT
    JMP NUM_LOOP
END_PROGRAM:
    MOV AX, ONES_COUNT  
    PUSH    AX
    RET
PARITY  ENDP
        END PARITY
Était-ce utile?

La solution

I assume that the intent of this is that PARITY will take an argument on the stack, and then modify that argument, leaving it in the same place on the stack:

; (This does not work, see below)

...
    PUSH    AX          ; input value
    CALL    PARITY      ; want this to change it in-place...
    ...
    POP     AX          ; ...so that this pops off the output value
    CALL    PUTDEC$

...

PARITY  PROC    FAR PUBLIC USES CX DX DS
    POP     AX          ; get argument from stack
    ...
    PUSH    AX          ; put result back on stack
    RET

This won't work, for two reasons:

  1. The CALL pushes a return address onto the stack.
  2. The prologue code generated by PROC ... USES pushes more stuff onto the stack.

When you POP AX inside PARITY, you're actually popping off something completely different (and then putting something else back in its place) - probably one of the other registers saved by the prologue.

Options for doing it right:

  1. If you understand that calling conventions being used here, you should be able to find the location of the AX that was pushed onto the stack, most likely at some small offset from the BP register (I'm not sufficiently familiar with MASM to know exactly what the PROC-generated prologue code does in this case), and load/store it directly.

  2. Or, make PARITY take its input and return its output in the AX register directly. In this case you would need to remove the PUSH AX before the CALL PARITY (the input is already in AX) and put it directly after the CALL instead (to save the returned result, which will be popped later - I'm assuming that AX needs to be saved because it may be corrupted by the other calls); and remove both the POP AX and PUSH AX from PARITY.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top