I’m trying to learn the basics of assembly but can’t get across on how to display results stored in memory.
section .data num1 db 1,2,3,4,5 num2 db 1,2,3,4,5 output: db 'The dot product is "' outputLen1 : equ $-output output2: db '" in Hex!', 10 output2Len : equ $-output2 section .bss dotProd resw 1 ; store dot product in here section .text global _start _start: mov eax, 0 mov ecx, 5 mov edi, 0 mov esi, 0 looper: mov ax, [edi + num1] mov dx, [esi + num2] mul dx add [dotProd], ax cmp cx, 1 je printOutput inc edi inc esi dec cx jmp looper ; go back to looper printOutput: mov eax,4 ; The system call for write (sys_write) mov ebx,1 ; File descriptor 1 - standard output mov ecx, output ; mov edx, outputLen1 ; int 80h ; Call the kernel mov eax, 4 mov ebx, 1 mov ecx, dotProd, mov edx, 1 int 80h mov eax, 4 mov ebx, 1 mov ecx, output2, mov edx, output2Len int 80h jmp done done: mov eax,1 ; The system call for exit (sys_exit) mov ebx,0 ; Exit with return code of 0 (no error) int 80h
What I’m trying to do is get the dot product of the two list of numbers and display it on the screen. However, I keep getting random letters which I believe are hex representations of the real decimal value. How can I convert it to decimal? The current value display is 7, which should is the equivalent ASCII char for 55, which in this case is the dot product of both list of numbers.
Advertisement
Answer
esi
andedi
must be increased such that it points to next element of array.(in this particular example, only one of them is sufficient).- declare
mun1
andnum2
asdd
, instead ofdb
(see here). Also, you have to have method for printing number.(see this and this).
Below is a complete code which uses printf
.
;file_name:test.asm ;assemble and link with: ;nasm -f elf test.asm && gcc -m32 -o test test.o extern printf %macro push_reg 0 push eax push ebx push ecx push edx %endmacro %macro pop_reg 0 pop edx pop ecx pop ebx pop eax %endmacro section .data num1: dd 1,2,3,4,5 num2: dd 1,2,3,4,5 msg: db "Dot product is %d",10,0 section .bss dotProd resd 1 ; store dot product in here section .text global main main: mov eax, 0 mov ecx, 5 mov edx, 0 mov esi, 0 mov dword[dotProd], 0h looper: mov eax, dword[esi + num1] mov edx, dword[esi + num2] mul edx add [dotProd], eax cmp cx, 1 je printOutput add esi,4 dec cx jmp looper ; go back to looper printOutput: push_reg push dword[dotProd] push dword msg call printf add esp,8 pop_reg jmp done done: mov eax,1 ; The system call for exit (sys_exit) mov ebx,0 ; Exit with return code of 0 (no error) int 80h