A
A
Arti-Jack2018-04-17 14:48:18
C++ / C#
Arti-Jack, 2018-04-17 14:48:18

What does cwb do and will the program calculate the expression correctly?

Good day.
The task is very trivial: calculation of the expression (22 / c + 3 * b), where c, b - are entered from the keyboard.
There are two files - crosses and asm. Since it will be easier to type numbers in C++ from the keyboard, in the second file I simply get these variables using extern. But this is not so important for the context of this problem.
I will omit the code of the first file, all you need to know in the context of this question is that numbers are entered and exported to the asm file.
Here is the ASM code:

MODEL Large, C
.data
Extrn b:byte, c:byte, numerator:word ; numenator - результат 

.code
Public Delim 
Delim proc far

; Подсчёт выражения
mov al, 3
imul b
; al = 3 * b

mov bx, ax
mov ax, 22
idiv c
; должно быть ax = 22 / c

cbw ;?
add bx, ax
mov ax, bx
; ax =22/c + 3*b
cbw ; Что делает этот мнемоник?
mov word ptr numenator, ax
mov word ptr numenator + 2, dx

ret
Delim endp
end

So, I don't understand if this works correctly?
This moment confuses me:
mov bx, ax
mov ax, 22
idiv c
; ax = 22 / c

In general, if you imagine this, it will come out:
5ad5de7120efe411121156.png
Then the sum of BX and AX is written to the BX register. But the BX register does not have the desired value.
And I also don’t quite understand, because the cbw mnemonic changes the value of the ax register to what is in al (I’m not entirely sure, but the compilation showed me just such a result).
So, my question is: Is there a mistake here at all (is it necessary to copy the values ​​of the AX register after 22 / a is written to AX), or is my logic wrong here? And yet, is it true that cbw writes the value of the AL register to the AX register? if it’s not difficult for you, then I will be glad if you explain step by step what is happening in general in order to make sure

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2018-04-17
@Arti-Jack

At first everything is correct:
- placed in AL 3 and multiplied by b, the result is in AX;
- recorded it in BX;
- placed in AX 22, divided by c, the quotient came out in AL;
- expanded the sign with pom. cbw, result in AX;
- added BX ( 3 * b) and AX (22 / s), and then wrote down in AX.
But then, instead of the second cbw, it was necessary to write cwd - to expand the AX sign to DX.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question