From: rio on

"Prime Mover" <epleite(a)hotmail.com> ha scritto nel messaggio
news:a86ba4bc-7db2-46d9-81ce-b36d4014d0da(a)d45g2000hsc.googlegroups.com...
>
> For instance, I also coudn't find a clear example of conversion of a
> simple program
> like this below in C to assembly:
>
> a=8;
> b=7;
> i=1;
> do
> {
> if (a<b)
> c = a + b*i;
> else
> c = a - b*i;
> i++;
> } while (i<=20);

for me "a" is "j"

j=8; b=7; i=1;
..0: j<b!#.1| a=b; c=j; mul i; c+=a; #.2;
..1: | a=b; c=j; mul i; c-=a;
..2: ++i; i<=20#.0;

something like
"
mov edi, 8
mov ebx, 7
mov esi, 1
..0: cmp edi, ebx
jb .1
mov eax, ebx
mov ecx, edi
mul esi
add ecx, eax
jmp short .2
..1: mov eax, ebx
mov ecx, edi
mul esi
add ecx, eax
..2: inc esi
cmp esi, 20
jbe .0
"

don't know if it is right this last above.

better this
j=8; b=7; i=1;
..0: a=b; c=j; mul i;
j<b!#.1| c+=a; #.2;
..1: | c-=a;
..2: ++i; i<=20#.0;


then because "a" and "b" are constants

j=8; b=7; i=1;
..0: a=b; c=j; mul i; c-=a;
++i; i<=20#.0;

than because i=20 is the last value

j=8; b=7; i=21; c=8-7*20;

> Thank you all.


From: Prime Mover on
Thank you all for all the information and tips. It is been really
helpful.

I am trying to convert this algorithm below to assembly:

%%%%%%%%%%%%
Unsigned Multiplication Algorithm

AQ = Q*M

A = 0;
Q = operand1
M = operand2
C = carry

for (count=1 to count <= nbits)

begin

if Q[0] = 1 then
A = A+M
end if

shift CAQ 1 bit to the right % CAQ is the C,A,Q grouped

end
%%%%%%%%%%%%%%%%%

I am doing like this, for 8-bit numbers:

mov AH, 0
mov AL, Q
mov BL, M
mov CL, 8; counter starts with 8
mov CF, 0; carry
L0:
cmp AL(0), 1; I need to access the first bit of AL!!!! don't know if
this is right
jne L1
add AH, BL
jmp L1
L1:
shr AX, 1
dec CF
cmp CF, 0
jng L0

Does that make sense???

Thank you again.
From: Prime Mover on
Something that occurred to me as I was posting the last post... what
if the numbers are just 4-bit numbers?
How could I manage to use the registers to perform that algorithm?? I
know this doesn't have much practical
use but that would help me to clarify some thins, I think.

Thank you all again.
From: Wolfgang Kern on

Prime Mover wrote:

> Thank you all for all the information and tips.
> It is been really helpful.

> I am trying to convert this algorithm below to assembly:
>
> %%%%%%%%%%%%
> Unsigned Multiplication Algorithm
>
> AQ = Q*M
>
> A = 0;
> Q = operand1
> M = operand2
> C = carry
>
> for (count=1 to count <= nbits)
>
> begin
>
> if Q[0] = 1 then
> A = A+M
> end if
>
> shift CAQ 1 bit to the right % CAQ is the C,A,Q grouped
>
> end
> %%%%%%%%%%%%%%%%%
>
> I am doing like this, for 8-bit numbers:
>
> mov AH, 0
> mov AL, Q
> mov BL, M
> mov CL, 8; counter starts with 8
> mov CF, 0; carry

The Carry-flag isn't a register, it is a bit in the flag-register
which become updated after many instructions by the CPU itself.
But we got three instructions to modify the CF direct:
CLC clear it
STC set it
CMC complement (toggle) it

> L0:
> cmp AL(0), 1
> I need to access the first bit of AL!!!! don't know if
> this is right

TEST AL, 1 ;is a non-operative AND AL,1,
;affects flags only ZF=Bit-status CF=0

> jne L1
> add AH, BL ;where shall your result go to ?
> jmp L1 ;jump to where ?
> L1:
> shr AX, 1 ; this also moves bit0 of AX into CF
; but it also shifts AH into AL
**> dec CF ; you can't decrement a bit
DEC CL ; may perhaps do what you want,
; DEC alters the Zero- and Sign-flag
;CMP CL,0 ; so this isn't required
JNZ L0
;> cmp CF, 0
;> jng L0 ;JNG is the signed option: jump if '<=0'

> Does that make sense???

If it should be a Shift-Add MUL loop: Not too much yet :)
perhaps easier to see if you start with Zero-extensions
and work with 16-bit junks:

MOV AL,Q
MOV AH,0 ;AX = Q
MOV DL,M
MOV DH,0 ;DX = M
MOV CX,8
MOV BX,0 ;BX = result, byte*byte is a word (16 bit)
L0:
SHR AX,1 ;bit0 goes to CF with this
JNC L1
ADD BX,DX
L1:
SHL DX,1 ;*2, because next bit in AX is double valued
DEC CX ;also possible with CX then: LOOP L0
JNZ L0 ;
done: ;result is in BX yet

Anyway I'd use the CPU-capabilities instead:
MOV AL,Q
MOV BL,M
MUL BL ;performs AX = AL*BL

But your example may be good for learning.

__
wolfgang



From: Wolfgang Kern on

"Prime Mover" asked:

> Something that occurred to me as I was posting the last post... what
> if the numbers are just 4-bit numbers?
> How could I manage to use the registers to perform that algorithm?? I
> know this doesn't have much practical
> use but that would help me to clarify some thins, I think.

Nibbles are of some use, think about HEX<->ASCII conversions ...
Just make your loop count 4 instead of 8 ?
or use the byte MUL instruction

assuming AL holds both factors (Q:M):

MOV AH,AL ;copy for split
SHR AH,4 ;move Q down (clears high four bits also)
AND AL,0Fh ;extract M
MUL AH ;AX=AL*AH
done: ;result: AL = Q*M AH=0
__
wolfgang