|
 |
|
 |
02-24-2006, 01:00 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Feb 2006
Posts: 3
|
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
|
|
|
02-24-2006, 01:29 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
|
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.
|
|
|
02-24-2006, 01:52 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Feb 2006
Posts: 3
|
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).
|
|
|
03-02-2006, 03:04 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Feb 2006
Posts: 3
|
Anyone else have any suggestions?
|
|
|
03-15-2006, 01:30 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Mar 2006
Posts: 3
|
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.
|
|
|
03-16-2006, 11:41 AM
|
#6 (permalink)
|
|
Jedi Master
Join Date: Feb 2006
Location: Denver, CO
Posts: 15
|
Deutsch:
Sure. Gimme a little context and I'll be happy to help.
-Carl
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:04 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|