0

Hello all. This past semester I took assembly programming. The programs in class were done on windwos machines and now I am trying to write some assembly on my linux machine, which has an intel 64bit processor. I am having a problem converting integers to ascii for output. I am using the same routine we used in my class which adds 30h to each digit and store it in a string but I am not getting the approptiate output. The number I am trying to output is 15 but I get D and a small black circle with a white question mark insid it. Does anyone know what is causing this? Is it an issue with using 64bit processor or is it an encoding issue in linux?

3
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by jt_murphree
0

an intel 64bit processor

Do you mean an actual 64 bit CPU? Like an Itanium? Or do you mean a normal x86 CPU with the EMT64 extensions?

0

it is a core 2 duo so if I remember correctly it has the 64 extensions. As for the code I will have to post it tonight. I do not have it with me right now.

0

Here is the code I have written. All I am trying to do at this point is add two numbers and convert the result to ascci and print to screen.

section	.data

msg	db	'Hello, world! this is jason.',0xa	;our dear string
len	equ	$ - msg			;length of our dear string
MAXSTR	equ	256d

section .bss
num1:	resd	1
num2:	resd	1
resultSt:	resb	MAXSTR
strAddr: 	resd	1
strLen:		resd	1
section	.text
    global _start			;must be declared for linker (ld)

_start:					;tell linker entry point

	mov eax, 5d
	mov ebx, 10d
	add eax, ebx
	mov dword [num1], eax
	
	lea esi, [num1]
	lea edi, [resultSt]
	call int2ascii
	
	lea esi, [resultSt]
	call printString

	;mov 	edx,len	;message length
	;mov	ecx,msg	;message to write
	;mov	ebx,1	;file descriptor (stdout)
	;mov	eax,4	;system call number (sys_write)
	;int	0x80	;call kernel

	mov	eax,1	;system call number (sys_exit)
	int	0x80	;call kernel

;********************procedure to convert int to ascii********************
;integer to be converted in esi, destination string in edi
;*************************************************************************
int2ascii:
	pushad		;push registers
	pushfd		;push flags
	cld		;clear direction flag
	
	mov eax, [esi]	;load number to eax
	mov ecx, 0h	;zero ecx for digit counter
	mov ebx, 10d	;move divisor of 10 to ebx
nextOut:
	mov edx, 0h	;zero edx for high order word of division
	div ebx		;divide by 10
	push edx	;push the remainder
	inc ecx		;count digit
	cmp eax, 0h	;compare eax to 0
	jg nextOut	;if greater jump to nextOut

charOut:
	pop eax		;get number from stack
	or eax, 0030h	;or with 30h to get int
	stosb		;store in destination string
	dec ecx		;decrease chars to print by one
	jnz charOut	;if CX > 0 go to charOut
	mov al, 0d	;mov 0 to al
	stosb		;terminate string
	popfd		;restore registers and flags
	popad
	ret

;***************************printString procedure**********************
;Given	 : string to print in esi
;process : print string using linux sys_call to print to screen
;	   no registers or flags are affected
;returns : nothing
;**********************************************************************
printString:
	pushad			;save flags and registers
	pushfd
	cld			;clear direction flag to print forward
	mov dword [strAddr], esi	;save string address
	mov dword [strLen], 0		;set length to 0

whileChar:	
	cmp BYTE [esi], 0		;compare string element to 0
	jz endWhileChar		;if zero end loop
	inc dword [strLen]		;increment string length
	inc esi			;increment esi
	jmp whileChar		;jump to start of loop

endWhileChar:
	mov edx, strLen
	mov ecx, strAddr	;string address
	mov ebx, 1		;stdout
	mov eax, 4		;syscall numbedr for write
	int 0x80		;call system

	popfd			;restore flags and registers
	popad	
	ret		;return to calling procedures
0

well I found one problem. I have to put the size for the variable in the print routine before making the syscall to write.
mov edx, dword [strLen]
mov ecx, dword [strAddr]

However, now I am getting no output after converting the number to a string. I am using nasm by the way.

0

ok I made one of those boneheaded mistakes. I made the changes mentioned in my last post and I was getting no output. I looked at the code for thirty minutes before noticing that I had commented out the call to the conversion procedure. Everything is working as it should noe. Thanks for all your help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.