Answer to Question #177338 in Assembler for aljoory

Question #177338

Implement the following C++ expression in assembly language, using 32-bit unsigned operands:

ask the user to enter val2, val1, val3 by calling Readint

If (val2 ==0) then

{val2 = 1}

val1 = (val3 / val2) * (val1 + val3)


Val1 = 600

Val2 = 0

Val3 = 1000

 


1
Expert's answer
2021-03-31T15:40:45-0400
TITLE  Assignment
INCLUDE Irvine32.inc

.data
; a message to display
enter_v1        BYTE "Enter val1: ",0
enter_v2        BYTE "Enter val2: ",0
enter_v3         BYTE "Enter val3: ",0
msgBad            BYTE "Invalid input, please enter again",0
msgResult        BYTE "The result is: ",0
val1    DWORD ?
val2    DWORD ?
val3    DWORD ?
.code
    main PROC
    call Clrscr                    ; Clears the screen
; Input val1
    mov edx, OFFSET enter_v1    ; String "Enter val1: "
    call WriteString            ; Write string
readVal1:
    call ReadInt                ; Read  val1
    jno  goodVal1                ; IF input is correct goto goodVal1
    mov  edx,OFFSET msgBad        ; ELSE String "Invalid input, please enter again"
    call WriteString            ; writes a string about Invalid input
    call crlf                    ; new string
    jmp  readVal1               ; goto input val1 again

goodVal1:                        ; va1 is correct
    mov    val1,eax                ; save val1

    mov edx, OFFSET enter_v2    ; String "Enter val2: "
    call WriteString            ; Write string
readVal2:
    call ReadInt                ; Read val2
    jno  goodVal2                ; IF input is correct goto goodVal2
    mov  edx,OFFSET msgBad        ; ELSE String "Invalid input, please enter again"
    call WriteString            ; writes a string about Invalid input
    call crlf                    ; new string
    jmp  readVal2               ; goto input val2 again

goodVal2:                       ; va2 is correct
   cmp eax,0                   ; IF(val2 == 0)
   jne continue                ; go to continue
    mov eax,1                  ; ELSE val2 = 1
continue:
    mov    val2,eax                ; save val2

    mov edx, OFFSET enter_v3    ; String "Enter val3: "
    call WriteString            ; Write string
readVal3:
    call ReadInt                ; Read  val3
    jno  goodVal3                ; IF input is correct goto goodVal3
    mov  edx,OFFSET msgBad        ; ELSE String "Invalid input, please enter again"
    call WriteString            ; writes a string about Invalid input
    call crlf                    ; new string
    jmp  readVal3               ; goto input val3 again

goodVal3:                        ; va2 is correct
    mov    val3,eax                ; save val3

; division: val3/val2
    xor edx,edx                    ; edx = 0 befor division
    mov    eax,val3                ; move val3  to eax (Divident)
    mov    ebx,val2                ; ebx = val2 (Divisor)
    div ebx                        ; eax = val3/val2

; add:  val1 + val3
   mov ecx, val1                ; move val1 to ecx
   add ecx, val3                ; ecx = val1 + val3

; multiply (val3/val2)*(val1 + val3)
  mul ecx                       ; eax = eax*ecx

  mov val1,eax                  ; save result to val1


    mov  edx,OFFSET msgResult   ; String "The result is: "
    call WriteString            ; writes string
    call WriteInt                ; display result

     call    crlf    
    call    crlf
    exit
main ENDP

END main

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS