Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

This asm code doesn't work, help me.

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.


[code]
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
[/code]

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]This code is definitely wrong:[/color]
    [code]
    ...
    mov [esi], eax
    inc esi
    ...
    mov [esi], eax
    inc esi
    [/code]
    [color=Blue]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:[/color]
    [code]
    ...
    mov [esi], eax
    add esi,4
    ...
    mov [esi], eax
    add esi,4
    [/code][color=Blue]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.[/color]
  • drew999drew999 Posts: 7Member
    I edited the code.



    [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
    [/code]
  • drew999drew999 Posts: 7Member
    The code still not work.
  • goweightlosgoweightlos Posts: 1Member
    see the book for x86 assembly guide
Sign In or Register to comment.