Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-24-2006, 01:00 PM   #1 (permalink)
gwalia
Registered User
 
Join Date: Feb 2006
Posts: 3
gwalia is on a distinguished road
Post Assembly Code for base conversion

I am working on an assembly code that can read a number in base 7, and print it to a base 9 number. The input of the program can be assumed to be a text file with proper base 7 numbers, one on each line.

I have the C code for what im trying to implement, but im unsure how i would go about doing it in assembly...any suggestions or help will be nice. Thank You in advance.

C Code:
Code:
#include <stdio.h>

int main(void)
{
  char ch;
  unsigned int value;
  unsigned int power9;
  
  while (fread(&ch, 1, 1, stdin))
  {
    value = 0;
    // if I am here, I read one character, not at EOF
    // and the character is read into ch
    do
    {
      if (ch != '\n') 
      {
        // if I am here, I just read a digit
        value = (value * 7) + (ch - '0');
      }
      else
      {
        // we are now at the end of line, nothing to do
      }
    } while ((ch != '\n') && (fread(&ch, 1, 1, stdin) != 0));
    // if I am here, we have a value read and interpreted
    printf("%u\n",value);
    power9 = 9*9*9*9*9;
    while (power9 > 0) 
    {
      unsigned quotient;

      quotient = value / power9;
      ch = quotient + '0';
      fwrite(&ch, 1, 1, stdout);
      value %= power9;
      power9 /= 9;
    }
    ch = '\n';
    fwrite(&ch, 1, 1, stdout);
  }
}

Last edited by redhead; 02-24-2006 at 01:08 PM. Reason: Added [code] tags
gwalia is offline   Reply With Quote
Old 02-24-2006, 01:29 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
redhead is on a distinguished road
I've got a question.. why
Code:
power9 = 9*9*9*9*9;
instead of:
Code:
power9 = 59049; // floor(pow(9,5)), or 9*9*9*9*9
That would save some cyckles, but most of your intelligent compilers will automaticaly sort that out.

With a bit of optimazation, gcc will give you something like this:
Code:
.LC0:
	.string	"%u\n"
	.text
	.p2align 4,,15
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%edi
	pushl	%esi
	pushl	%ebx
	subl	$28, %esp
	andl	$-16, %esp
	.p2align 4,,15
.L2:
	movl	stdin, %ebx
	movl	$1, %eax
	movl	$1, %ecx
	movl	%eax, 8(%esp)
	leal	-13(%ebp), %edx
	movl	%ecx, 4(%esp)
	movl	%ebx, 12(%esp)
	xorl	%ebx, %ebx
	movl	%edx, (%esp)
	call	fread
	testl	%eax, %eax
	je	.L19
	.p2align 4,,15
.L5:
	movzbl	-13(%ebp), %edx
	cmpb	$10, %dl
	je	.L6
	leal	0(,%ebx,8), %ecx
	movl	stdin, %esi
	movsbl	%dl,%edi
	subl	%ebx, %ecx
	movl	$1, %edx
	leal	-48(%edi,%ecx), %ebx
	movl	%edx, 4(%esp)
	movl	$1, %ecx
	leal	-13(%ebp), %edx
	movl	%esi, 12(%esp)
	movl	%ecx, 8(%esp)
	movl	%edx, (%esp)
	call	fread
	testl	%eax, %eax
	jne	.L5
.L6:
	movl	%ebx, 4(%esp)
	movl	$59049, %esi
	movl	$954437177, %edi
	movl	$.LC0, (%esp)
	call	printf
	.p2align 4,,15
.L15:
	movl	%ebx, %eax
	xorl	%edx, %edx
	movl	stdout, %ecx
	divl	%esi
	movl	%ecx, 12(%esp)
	movl	$1, %ecx
	movl	%ecx, 4(%esp)
	movl	%edx, %ebx
	addb	$48, %al
	movb	%al, -13(%ebp)
	movl	$1, %edx
	movl	%edx, 8(%esp)
	leal	-13(%ebp), %edx
	movl	%edx, (%esp)
	call	fwrite
	movl	%esi, %eax
	mull	%edi
	shrl	%edx
	testl	%edx, %edx
	movl	%edx, %esi
	jne	.L15
	movb	$10, -13(%ebp)
	movl	stdout, %edi
	movl	$1, %esi
	movl	%esi, 8(%esp)
	movl	$1, %ebx
	leal	-13(%ebp), %eax
	movl	%ebx, 4(%esp)
	movl	%edi, 12(%esp)
	movl	%eax, (%esp)
	call	fwrite
	jmp	.L2
.L19:
	leal	-12(%ebp), %esp
	popl	%ebx
	popl	%esi
	popl	%edi
	popl	%ebp
	ret
	.size	main, .-main
Now with hand uptimazation there are a few instructions which could be eliminated, but execution time would probably be unaffected.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 02-24-2006, 01:52 PM   #3 (permalink)
gwalia
Registered User
 
Join Date: Feb 2006
Posts: 3
gwalia is on a distinguished road
I think what you have replaced 9*9*9*9*9 with is just fine. I am using gdb as my compiler. Also, i dont know if this might help but there are other requirements as well.

Requirements are:

The input of the program can be assumed to be a text file with proper base 7 numbers, one on each line. You can assume each number starts on a line without leading spaces, and ends on a line without trailing spaces. In other words, each number begins on column 0, and is terminated by the linefeed (newline) character (ASCII 10).

The output of the program should be fixed width base 9 numbers. Each number should have exactly 11 digits (including leading zeros).
gwalia is offline   Reply With Quote
Old 03-02-2006, 03:04 PM   #4 (permalink)
gwalia
Registered User
 
Join Date: Feb 2006
Posts: 3
gwalia is on a distinguished road
Anyone else have any suggestions?
gwalia is offline   Reply With Quote
Old 03-15-2006, 01:30 PM   #5 (permalink)
deutsch
Registered User
 
Join Date: Mar 2006
Posts: 3
deutsch is on a distinguished road
Assembly help

I am still fairly new to assembly, did C and C++ for awhile.

I couldn't figure how to post a new topic.

Would there be someone who could help me with some
Win32 assembly code ?

Having problems setting a registry key.

Thanks.
deutsch is offline   Reply With Quote
Old 03-16-2006, 11:41 AM   #6 (permalink)
Hamlin
Jedi Master
 
Hamlin's Avatar
 
Join Date: Feb 2006
Location: Denver, CO
Posts: 15
Hamlin is on a distinguished road
Deutsch:

Sure. Gimme a little context and I'll be happy to help.

-Carl
Hamlin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hey guys! Code malfunction - need experts workover groundfrog Platform/API C++ 1 06-09-2005 07:46 AM
how the servlet will integrate the LDAP code j.gohel Java 19 04-16-2005 12:55 AM
Cisco Code breaking sde Code Newbie News 0 05-21-2004 07:10 AM
Microsoft probes Windows code leak redhead Code Newbie News 0 02-13-2004 12:41 AM


All times are GMT -8. The time now is 10:04 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting