0

I have code that got two inputs from the user and multiplied them. As you can see below.

I tried to split things up in to separate procedures and then call them all at the end.

I do though have some questions about what I did. After the first compare in each getValue procedure I understood that i needed to jump to the next compare but after the second compare I didn't know where to jump to.

Does it makes sense to jump after the second compare to the start of the next procedure or should i just be jumping down to the return or end procedure statement?

I also wanted a second opinion on if it would makes sense to just simply have one getValue procedure and then call it twice at the end instead of calling two separate procedures that do the same thing.

It says that it builds successfully but when I run it just says "press any button to continue...." instead of coming up with the code so I don't know whats up with that.

INCLUDE Irvine32.inc
.data
    intVal1 SDWORD ? ; User input one
    intVal2 SDWORD ? ; User input two
    prompt  BYTE "The result of multiplying the numbers is ",0
    prompt2 BYTE "Enter a number that will be multiplied",0
    prompt3 BYTE "Error. Number not in range.",0

.code

; Error procedure displays error prompt
error PROC
    mov edx, OFFSET prompt3
    call WriteString
ret
error ENDP

; GetValue1 procedure will get first input from user and display error      and end program when input not in range
GetValue1 PROC
    mov edx, OFFSET prompt2
    call WriteString    ; tells the user to enter a number
    call ReadInt
    mov intVal1,eax     ; save the returned value from eax to intVal1
    cmp intVal1, 32767
    jl Loop1      ; if intVal1 is less than 32767 jump to Loop1, otherwise continue with error procedure
    call error
    jmp end 
    Loop1: cmp intVal1, -32768
    jg Loop2         ; if intVal1 is greater than -32768 jump to Loop2,  otherwise continue with error procedure
    call error
    jmp end 
    ret
GetValue1 ENDP

; GetValue2 procedure will do the same thing as GetValue1
GetValue2 PROC
    Loop2: mov edx, OFFSET prompt2
    call WriteString
    call ReadInt
    mov intVal2,eax
    cmp intVal1, 32767
    jl Loop3      ; if intVal1 is less than 32767 jump to Loop3, otherwise     continue with error procedure
    call error
    jmp end 
    Loop3: cmp intVal1, -32768
    jg Loop4        ; if intVal1 is greater than -32768 jump to Loop4, otherwise continue with error procedure
    call error
    jmp end 
    ret
GetValue2 ENDP

; MultiplyAndDisplay will multiply the inputs and display the result along with a prompt
MultiplyAndDisplay PROC
    Loop4: imul eax, intVal1     ; signed multiply of eax by intVal1                 
    mov edx, OFFSET prompt
    call WriteString      ; Writes the prompt in edx
    call WriteDec         ; Writes the value in eax
    ret
MultiplyAndDisplay ENDP

main PROC
    call GetValue1
    call GetValue2
    call MultiplyAndDisplay
    exit
main ENDP
END main
rkhb
  • 14,159
  • 7
  • 32
  • 60
  • Edited to removed sentences that did not add any details to the question. Added paragraph breaks for readability. – Dijkgraaf Apr 04 '17 at 04:13

1 Answers1

0

Use a single procedure to get values. Have the procedure return the value in eax. Do not use jumps that attempt to jump from one procedure to another procedure. The main code could be:

main PROC
    call GetValue            ;eax == first value
    push eax                 ;save first value
    call GetValue            ;eax == second value
    pop  ebx                 ;ebx == first value
    call MultiplyAndDisplay  ;change this to multiply eax by ebx
    exit
main ENDP
rcgldr
  • 27,407
  • 3
  • 36
  • 61