1 Replies - 22385 Views - Last Post: 12 June 2009 - 01:43 PM

#1 future_ahead   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-November 08

How to print an array of real numbers in assembly

Posted 12 June 2009 - 05:18 AM

I have a program in C++ like this:
float a[]={1.1,2.2,3.3,4.5,5.2};
for (int i=4;i>=0;i--)
	   printf("%f ", a[i]);

Please tell me how to translate it into assembly language.
I can do this if array a has type int*. And it runs well.
But in this situation my array a has type float*. I dont know how to translate it. I think that I need to use coprocessor-instructions, which work with real numbers.

My program for printing array of integer numbers like this: (embedded assembly in MS visual studio 2008)
#include "stdafx.h"
#include <conio.h>
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
	/* Print to screen array of integers in format: 
a[4] a[3] a[2] a[1] a[0] */
	int a[5]={10,20,30,40,50};
	char myformat[]="%d ";
	__asm {
			   //ecx = index of array a, ecx in [4,3,2,1,0]
			push ecx
			dec ecx
			mov ebx,dword ptr [a+ecx*4]	//sizeof(int)=4 bytes
			push ebx
			//push myformat
			lea eax, myformat
			push EAX
			//print ebx in myformat, i. e. printf("%d",ebx)
			call dword ptr[printf]																 
			call dword ptr[_getch]
			pop EAX
			pop EBX
	return 0;

Thanks you very much for your help.

This post has been edited by future_ahead: 12 June 2009 - 06:17 AM

Is This A Good Question/Topic? 0
  • +

Replies To: How to print an array of real numbers in assembly

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to print an array of real numbers in assembly

Posted 12 June 2009 - 01:43 PM

Ask your compiler:
@@displayArray$qpfi	proc	near
	push	  ebp
	mov	   ebp,esp
	push	  ebx
	push	  esi
	mov	   esi,dword ptr [ebp+8]
;		for(int i = size; i >= 0; i--) {
; ESI = a
	mov	   ebx,dword ptr [ebp+12]
	test	  ebx,ebx
	jl		short @7
;			printf(displayFormat, a[i]);
; EBX = i, ESI = a
	fld	   dword ptr [esi+4*ebx]
	add	   esp,-8
	fstp	  qword ptr [esp]
	push	  dword ptr [_displayFormat]
	call	  @_printf
	add	   esp,12
	dec	   ebx
	test	  ebx,ebx
	jge	   short @6
	pop	   esi
	pop	   ebx
	pop	   ebp
@@displayArray$qpfi	endp

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1