Author Topic: Getting stuff to output  (Read 10995 times)

MAugustyniak

  • Guest
Getting stuff to output
« on: October 09, 2005, 04:06:22 AM »
Hi! I'm doing my class notes at home and I can't get it to work-out the following:

I want to output the integer result of the dot product. The code is:

;; This program calculates the dot product of two vectors
segment .data                   ; the data part declares initialized variables
        v1      db      1,2,3,4,5,6
        v2      db      5,6,7,8,9,0
        msg dw 'The value of the dot product is:',0xA
        len equ $-msg
segment .bss
        dotprod resw    1
segment .text
        global  _start
_start:
        mov ecx,6               ; here we initialize the count to the number of elements
        sub esi,esi             ; basically sets esi to 0
        sub dx,dx
cont:   mov al,[esi+v1]         ; esi is 0 and v1 refers tot he first index of v1
        mov bl,[esi+v2]         ; we do the same thing to bl, we'll multiply the two
        imul bl                 ; 8 bit multiplication consists of multiplying the argument of
        ;; imul and al. The result goes to ax which is 16 bits, hence 2x the size of either factor
        add dx,ax               ; dx holds the dot product, hence adding ax will to it adds
        ;; the result of the ith product
        inc esi                 ; increments esi
        loop cont               ; will do it ecx times, hence 6 times
        mov [dotprod],dx        ; assigns the dot product to its space in memory

;; and now for output
        mov eax,4
        mov ebx,1
        mov ecx,msg
        mov edx,len             ; helps end properly and avoids extra characters
        int 0x80

mov eax,4
        mov ebx,1
        mov dword ecx,[dotprod]
        mov edx, 1
        int 0x80

exit:
        mov eax,1
        int 0x80

I only get the text message as output, the value itself doesn't show up at all.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Getting stuff to output
« Reply #1 on: October 09, 2005, 05:19:53 AM »
(so far, so good)
        ...
        mov eax,4
        mov ebx,1
        mov dword ecx,[dotprod]
        mov edx, 1
        int 0x80

What do you expect this to do? We want the address of the buffer to write from in ecx - you've got the dot product... plus a couple of "garbage" bytes, since [dotprod] is a word and ecx is a dword. Lord knows what might be at that address - "SIGSEGV" probably :)

What we *want* to do here is print "64" (or whatever the answer is - my matrix algebra is rusty). Two characters, a '6' and a '4'. We want to get these two characters into a buffer, and point ecx at that buffer for the sys_write. In this case, edx wants to be 2, but that'll vary.

Here's one way you could do it - perhaps not the best. This displays *all* of eax. You might want to "fix" that - "movzx eax, word [dotprod]" - or make your variable a dword... (the "top" label was really a "local" label, ".top:", but this forum butchers that syntax... "jnz.top" becomes "jnz.top"... sigh...)

Best,
Frank

;---------------------------------
showeaxd:
    push eax
    push ebx
    push ecx
    push edx
    push esi

sub esp, 10h
    lea ecx, [esp + 12]
    mov ebx, 10
    xor esi, esi
    mov byte [ecx], 0
top:
    dec ecx
    xor edx, edx
    div ebx
    add dl, '0'
    mov [ecx], dl
    inc esi
    or eax, eax
    jnz top

mov edx, esi
    mov ebx, 1
    mov eax, 4
    int 80h

add esp, 10h

pop esi
    pop edx
    pop ecx
    pop ebx
    pop eax

ret
;---------------------------------

nobody

  • Guest
Re: Getting stuff to output
« Reply #2 on: October 09, 2005, 11:17:14 AM »
Still not getting anything.
I tried yours, then I also tried a bunch of other things like
mov cx, [dotprod].
Either way, I don't even get any garbage bits.
I've got another question, and I am totally new to this, but is it possible that the first ooutput block is interfering with the out put of the result of the dot product?

Maybe I need a bit of a tutorial in outputting.

By the way, the dot product is the sum of the products of the corresponding entries in two vectors. Hence the sum of the products first, andded to the second, .., and the nth pairs

I.e.

v1 = 1,2,3,4,5
v2 = 5,6,7,8,9

v1.v2 = 1*5 + 2*6 + 3*7 + 4*8 + 5*9 , and so on...