Answer the question
In order to leave comments, you need to log in
How to work with negative numbers in assembler?
Goal: Compute (2*a/b-1)/(a-28+c) in an assembler function called from a C++ file.
Problem: an assembler function from time to time perceives a certain number not as negative in the additional code or as it is there, but as positive.
This is how the parameters are declared in the C++ file:
extern "C"
{
signed __int8 a = 0, b = 1, c = 29;
signed __int8 numerator = 0;
signed __int16 denominator = 0;
signed __int8 result = 0;
void ASM_count();
}
;.586
;.model large, C
.data
extern a:sbyte
extern b:sbyte
extern c:sbyte
extern numerator:sword
extern denominator:sbyte
extern result:sword
;(2*a/b-1)/(a-28+c)
.code
ASM_count PROC
;///////////////////////////////numerator///////////////////////////////
xor eax, eax
mov al, 2
imul a
;cbw
idiv b
dec ax
mov numerator, ax
;///////////////////////////////denominator///////////////////////////////
xor eax, eax
mov al, a
sub al, 28
add al, c
mov denominator, al
;///////////////////////////////result///////////////////////////////
xor eax, eax
mov ax, numerator
idiv denominator
mov result, ax
ret
ASM_count endp
end
Answer the question
In order to leave comments, you need to log in
signed __int8 numerator = 0; signed __int16 denominator = 0;
extern numerator:sword extern denominator:sbyte ... mov denominator, al
__int8 numerator
with __int16 numerator
, extern denominator:sbyte
with extern denominator:sword
and mov denominator, al
with mov denominator, ax
, so that it becomes as it should. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question