Assembler Developer

Moderators: None (Apply to moderate this forum)
Number of threads: 1018
Number of posts: 1812

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
This asm code doesn't work, help me. Posted by drew999 on 18 Apr 2011 at 1:01 AM
I've tried to write a program that perform multiplications, using shl.
This code should multiply any numbers from 0 to 255 for n factors, but doesn't work: lcs is repeated twice and the result is wrong.


max TEXTEQU <100>
.data
p0 BYTE "Inserisci il primo fattore: ", 0
p1 BYTE "Inserisci l'altro fattore: ", 0
p2 BYTE "Vuoi inserire un altro fattore?[s/n]: ", 0
fattori BYTE max dup(0)
shift BYTE 7 dup(0)
.code
MAIN PROC
mov esi, offset fattori
mov edx, offset p0
call WriteString
call ReadInt
call Crlf
mov [esi], eax
inc esi
mov edx, offset p1
call WriteString
call ReadInt
call Crlf
mov [esi], eax
lr:
inc esi
mov edx, offset p2
call WriteString
call ReadInt
mov [esi], eax
call Crlf
cmp [fattori + lengthof fattori], 1
call MOL
cmp al, "s"
je lr
call MOL
mov eax, [esi]
call WriteInt
mov esi, offset fattori
mov ecx, 100
call DumpMem
exit
MAIN ENDP

MOL PROC
mov ecx, esi
sub ecx, offset fattori
dec ecx
mov esi, offset fattori
inc esi
l1:
push ecx
mov ecx, 7
mov al, [esi - 1]
mov bh, [esi]
shr bh, 1
jnc lcs
mov bl, 1
lcs:
mov bh, [esi]
cmp ecx, 0
je lal
dec ecx
shr bh, 1
jnc lcs
mov [shift + ecx], 1
inc ecx
loop lcs
lal:
call DumpRegs
mov ecx, 7
ls:
push ecx
mov cl, [shift + ecx]
shl al, cl
pop ecx
loop ls
mov [esi], al
inc esi
cmp bl, 1
jne lll
mov edx, [esi]
add edx, [esi - 1]
mov [esi], edx
lll:
pop ecx
loop l1
lret:
ret
MOL ENDP
END MAIN

Report
Re: This asm code doesn't work, help me. Posted by AsmGuru62 on 18 Apr 2011 at 4:19 AM
This code is definitely wrong:
...
mov [esi], eax
inc esi
...
mov [esi], eax
inc esi

When you write EAX at address in ESI - CPU writes 4 bytes, because EAX is 4 bytes long. Then you increment the address by 1 byte and then when you write EAX next time - the code OVERWRITES 3 bytes of the previous value with new value. You need following code:
...
mov [esi], eax
add esi,4
...
mov [esi], eax
add esi,4
No doubt that when you read those values back to use them you also have to use offset of 4 bytes multiplied by index of the value.
Report
Re: This asm code doesn't work, help me. Posted by drew999 on 18 Apr 2011 at 4:48 AM
I edited the code.



max TEXTEQU <400>
.data
p0 BYTE "Inserisci il primo fattore: ", 0
p1 BYTE "Inserisci l'altro fattore: ", 0
p2 BYTE "Vuoi inserire un altro fattore?[s/n]: ", 0
fattori BYTE max dup(0)
shift BYTE 7 dup(0)
.code
MAIN PROC
mov esi, offset fattori
mov edx, offset p0
call WriteString
call ReadInt
call Crlf
mov [esi], al
inc esi
ladd:
mov edx, offset p1
call WriteString
call ReadInt
call Crlf
mov [esi], al
inc esi
lr:
mov edx, offset p2
call WriteString
call ReadChar
call WriteChar
call Crlf
cmp al, "s"
je ladd
call MOL
mov eax, [esi]
call WriteInt
exit
MAIN ENDP

MOL PROC
mov ecx, esi
sub ecx, offset fattori
dec ecx
mov esi, offset fattori
inc esi
l1:
push ecx
mov ecx, 7
mov al, [esi - 1]
mov ah, [esi]
shr ah, 1
jnc lcs
mov bl, 1
lcs:
mov ah, [esi]
cmp ecx, 0
je lal
dec ecx
shr ah, 1
jnc lcs
mov [shift + ecx], 1
inc ecx
loop lcs
lal:
mov ecx, 7
jmp ls
ls:
push ecx
mov cl, [shift + ecx]
shl al, cl
pop ecx
loop ls
mov [esi], al
inc esi
cmp bl, 1
jne lll
mov edx, [esi]
add edx, [esi - 1]
mov [esi], edx
lll:
pop ecx
loop l1
lret:
ret
MOL ENDP
END MAIN

Report
Re: This asm code doesn't work, help me. Posted by drew999 on 18 Apr 2011 at 10:28 AM
The code still not work.
Report
Re: This asm code doesn't work, help me. Posted by goweightlos on 17 May 2011 at 4:22 AM
see the book for x86 assembly guide



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.