studentck
studentck

Reputation: 53

To find the MIN value in an array of 3 - MASM

Can someone help me figure out why this code isn't working? I received an error on the 3 lines with the compare instructions. This is for class and I have to teach myself basically, so any help would be appreciated. This assignment is for a chapter on passing values and analyzing the stack.

; procedure to compute the minimum of 3 DWORD values
; The MIN is calculated, returned in eax, and displayed.
; Other registers are unchanged.

.386            ; assembler use 80386 instructions
.MODEL  FLAT    ; use modern standard memory model 

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

INCLUDE io.h    ; header file for input/output

cr      EQU     0dh     ; carriage return
Lf      EQU     0ah     ; line feed

.STACK  4096    ; reserve 4096-byte stack

.DATA   ; reserve storage for data

num1    DWORD   0
num2    DWORD   0
num3    DWORD   0

directions  BYTE     cr, Lf, 'Please enter 3 numbers.', cr, Lf
            BYTE     'This program will then report the minimum',cr,Lf, 0
numlabel    BYTE        cr,Lf,Lf, 'The MIN is:  '
minValue    BYTE     16 DUP (?), cr,Lf,0    

.CODE   ; program code

MIN3  PROC   NEAR32

    push    ebp         ; save base pointer
    mov     ebp,esp     ; copy stack pointer
    push    ebx         ; save registers
    push    ecx
    push    edx

    cmp     num1, num2
    jg      second
    cmp     num1, num3
    jg      second
    mov     eax, num1
    jmp     retpop

second:
    cmp     num2, num3
    jg      third
    mov     eax, num2
    jmp     retpop

third:
    mov     eax, num3
    jmp     retpop

retpop: 
    pop     edx         ; restore registers
    pop     ecx 
    pop     ebx          
    pop     ebp         ; restore base pointer
    ret                 ; return

MIN3  ENDP

_start:     ; program entry point

    output  directions      ; display directions
    input   minValue,16     ; get number
    atod    minValue        ; convert to integer
    mov     num1, eax
    input   minValue,16     ; get number
    atod    minValue        ; convert to integer
    mov     num2, eax
    input   minValue,16     ; get number
    atod    minValue        ; convert to integer
    mov     num3, eax

    call   MIN3             ; find minimum value, ret eax

    dtoa    minValue, eax
    output  numlabel


INVOKE ExitProcess, 0   ; exit with return code 0
PUBLIC _start           
END

Upvotes: 0

Views: 2060

Answers (1)

Jester
Jester

Reputation: 58772

x86 has no version of the cmp instruction that supports 2 memory operands. As such, cmp num1, num2 is invalid, you should load one of them into a register and use that for the comparison, for example:

mov edx, num1
cmp edx, num2

You should pay attention to what the assembler is trying to tell you through the error message and also have the instruction set reference handy.

Upvotes: 3

Related Questions