8086 The following program runs the controller for electric motor control and si
ID: 3696164 • Letter: 8
Question
8086
The following program runs the controller for electric motor control and simulated motor behavior. The program is correct when the value of “error” ( error = measuredSpeed – requestedSpeed ) is positive, but negative numbers for error are not printed correctly. Run the program as is, with initial voltage values of 100v (for negative error) and 140v (for positive error) to see the issue with printing negative numbers.
Modify the following program so that it can print the negative numbers for the value of “error” correctly. To test the program, use initial voltage of 100 to see negative numbers and 140 to see positive numbers. You can either change PrintAX procedure, or write a new procedure similar to PrintAX, and call it if error is negative.
org 100h
.DATA
;jmp not needed if .CODE is specified below, assembler automatically inserts a JMP
voltage DB ?
measuredSpeed DW ?
requestedspeed DW ? ; read from port 1 and copy here
somebytes DB 6 DUP(0AAH)
starttext DB 'starting controller. Speed error: $'
finalmsg DB ' Final voltage value is : $'
.CODE
start:
MOV DX,OFFSET starttext;
MOV AH,9 ; call int 21H with this parameter to print what's at starttext
INT 21H
;initialize control settings
MOV voltage, 100 ; initial voltage value is set here
maincontrolloop:
CALL PrintNewl
IN AX, 01H ; read user's requested speed from port 1
MOV requestedspeed, AX
;calculate new voltage value to be set
;TODO
MOV AL,voltage ;set voltage value on port 5
OUT 5, AL
CALL motormodel ; this call simulates motor behavior
IN AX, 6; read current speed from port 6
MOV measuredSpeed, AX
;print the error between current speed and user request ; change this to print negative numbers correctly.
MOV AX, measuredSpeed
SUB AX, requestedspeed
CALL PrintAX ; prints positive error values
;calculate error
MOV AX, measuredSpeed
SUB AX, requestedspeed
; calculate new control input (voltage), AL has the error in it
CMP AX,0
JL increaseV
JG decreaseV ; if measuredSpeed > requestedspeed
JE nochange
increaseV:
INC voltage
JMP maincontrolloop
decreaseV:
DEC voltage
JMP maincontrolloop
nochange:
CALL PrintNewl
MOV DX,OFFSET finalmsg;
; end here
MOV AH,9 ; call int 21H with this parameter to print what's at starttext
INT 21H
MOV AH,0
MOv AL, voltage
CALL PrintAX
;JMP maincontrolloop ; ideally we need this jump, but for now we can disable it
HLT
; =========================== PROCEDURES
motormodel PROC ; given voltage as input, this procedure simulates speed value
; formulae : S = V*20 + 100 ; speed in RPM
IN AL, 05H ; read voltage from input port 05
;MOV AL, voltage
MOV BL, 20
MUL BL ; result of AL*12 will be in AX
;ADD AX, 100 ; for a good motor
ADD AX, 40 ; for a rusty motor
OUT 06, AX ;send result, in AX, to output port 6
;MOV speed, AX
RET
ENDP
Printnewl PROC
MOV DL, 0DH ; or 0AH for new line
MOV AH, 2
INT 21H
MOV DL, 0AH ; or 0AH for new line
MOV AH, 2
INT 21H
RET
ENDP
PrintAX PROC ; print what's in AX
MOV BL, 100
IDIV BL ; quotient will be in AL, and remainder in AH
PUSH AX ; move remainder AH to stack
MOV DL,AL ; print the first digit
ADD DL,30H
MOV AH,2
INT 21H
POP AX ; get remainder from stack
MOV AL,AH
MOV AH,0
MOV BL, 10
IDIV BL
PUSH AX ; move remainder to stack
MOV DL,AL ; print the 2nd digit
ADD DL,30H
MOV AH,2
INT 21H
POP AX ; get remainder from stack
MOV AL,AH
MOV AH,0
MOV DL,AL ; print the 2nd digit
ADD DL,30H
MOV AH,2
INT 21H
RET
ENDP
END start
Explanation / Answer
org 100h
.DATA
;jmp not needed if .CODE is specified below, assembler automatically inserts a JMP
voltage DB ?
measuredSpeed DW ?
requestedspeed DW ? ; read from port 1 and copy here
somebytes DB 6 DUP(0AAH)
starttext DB 'starting controller. Speed error: $'
finalmsg DB ' Final voltage value is : $'
.CODE
start:
MOV DX,OFFSET starttext;
MOV AH,9 ; call int 21H with this parameter to print what's at starttext
INT 21H
;initialize control settings
MOV voltage, 100 ; initial voltage value is set here
maincontrolloop:
CALL PrintNewl
IN AX, 01H ; read user's requested speed from port 1
MOV requestedspeed, AX
;calculate new voltage value to be set
;TODO
MOV AL,voltage ;set voltage value on port 5
OUT 5, AL
CALL motormodel ; this call simulates motor behavior
IN AX, 6; read current speed from port 6
MOV measuredSpeed, AX
;print the error between current speed and user request ; change this to print negative numbers correctly.
MOV AX, measuredSpeed
SUB AX, requestedspeed
CMP AX,0
JG printpostive
JL printnegative
;calculate error
MOV AX, measuredSpeed
SUB AX, requestedspeed
; calculate new control input (voltage), AL has the error in it
CMP AX,0
JL increaseV
JG decreaseV ; if measuredSpeed > requestedspeed
JE nochange
increaseV:
INC voltage
JMP maincontrolloop
decreaseV:
DEC voltage
JMP maincontrolloop
nochange:
CALL PrintNewl
MOV DX,OFFSET finalmsg;
printpostive:
CALL PrintAX ; prints positive error values
printnegative:
CALL PrintAXD ; prints negative error values
; end here
MOV AH,9 ; call int 21H with this parameter to print what's at starttext
INT 21H
MOV AH,0
MOv AL, voltage
CALL PrintAX
;JMP maincontrolloop ; ideally we need this jump, but for now we can disable it
HLT
; =========================== PROCEDURES
motormodel PROC ; given voltage as input, this procedure simulates speed value
; formulae : S = V*20 + 100 ; speed in RPM
IN AL, 05H ; read voltage from input port 05
;MOV AL, voltage
MOV BL, 20
MUL BL ; result of AL*12 will be in AX
;ADD AX, 100 ; for a good motor
ADD AX, 40 ; for a rusty motor
OUT 06, AX ;send result, in AX, to output port 6
;MOV speed, AX
RET
ENDP
Printnewl PROC
MOV DL, 0DH ; or 0AH for new line
MOV AH, 2
INT 21H
MOV DL, 0AH ; or 0AH for new line
MOV AH, 2
INT 21H
RET
ENDP
PrintAX PROC ; print what's in AX postive case
MOV BL, 100
IDIV BL ; quotient will be in AL, and remainder in AH
PUSH AX ; move remainder AH to stack
MOV DL,AL ; print the first digit
ADD DL,30H
MOV AH,2
INT 21H
POP AX ; get remainder from stack
MOV AL,AH
MOV AH,0
MOV BL, 10
IDIV BL
PUSH AX ; move remainder to stack
MOV DL,AL ; print the 2nd digit
ADD DL,30H
MOV AH,2
INT 21H
POP AX ; get remainder from stack
MOV AL,AH
MOV AH,0
MOV DL,AL ; print the 2nd digit
ADD DL,30H
MOV AH,2
INT 21H
RET
ENDP
PrintAXD PROC ; print what's in AX negative case
PUSH AX
PUSH BX
PUSH CX
PUSH DX
CMP AX, 0
JGE L1
MOV BX, AX
MOV DL, '-'
MOV AH, 2
INT 21H
MOV AX, BX
NEG AX
CALL PrintAXD
JMP L3
L1:
MOV DX, 0
MOV CX, 10
DIV CX
CMP AX, 0
JNE L2
ADD DL, '0'
MOV AH, 2
INT 21H
JMP L3
L2:
CALL PrintAXD
ADD DL, '0'
MOV AH, 2
INT 21H
L3:
POP DX
POP CX
POP BX
POP AX
RET
ENDP
END start
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.