MarkLim
unread,Apr 10, 2012, 1:56:58 AM4/10/12You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
Hi everyone,
Last year I started to learn the basics of i386/x86-64 assembly
language using NASM armed with an online book written by Dr Paul
Carter. It goes without saying a beginner needs to see an output of
the result(s) of their programs. It is not a trivial thing for
beginners to write their I/O routines. Since the day of the original
Apple II, COUT ($FDED) had been used to display output by assembly
language programmers.
Carter's book recommended using the std library c-function "printf". I
wanted to find out if we could come up with something similar using
ORCA/M and ORCA/C under ORCA shell. The programs below are the results
of these experiments. First, I wrote a simple program which every C
programmer knows.
#include <stdio.h>
int main()
{
printf("Hello, world\n");
}
Compiling it gives 2 files, hello.root and hello.a. Then I did an
object dump on hello.root.
DUMPOBJ +D -H hello.root >tmp.txt
If you take a look at the tmp.txt file, you would discover this is the
file that starts up a C program in the ORCA shell environment , call a
function labelled main and finally shutdown. In fact, compiling any C
file using ORCA/C alway produces a .root file. (Reference: page 21
ORCA/C manual.) The second part of the program is how to call a C
function from Assembly Language. Page 55 of the manual says it is very
simple; only a code snippet to help us out.
Using these ideas, I wrote 2 programs. The main program just add two
numbers and call a C function called foo to display the results.
#include <stdio.h>
int foo (long num1, long num2, long *sum)
{
printf("%ld + %ld = %ld\n", num1, num2, *sum);
}
mcopy main.macs
OBJCASE ON
main start
clc
lda num1
adc num2
sta sum
lda num1+2
adc num2+2
sta sum+2
pushlong #sum
pushlong num2
pushlong num1
jsl foo
rtl
num1 dc i4'2'
num2 dc i4'3'
sum ds 4
end
You can link the object files by typing:
link foo main keep=test
Next to find out if ORCA/C behaves like any C compiler by passing
parameters to the function main. This will be useful later if we want
our assembly language program to get parameters issued at the ORCA
shell command line.
#include <stdio.h>
int foo (int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
printf("%d %s\n", i, argv[i]);
}
mcopy main.macs
OBJCASE ON
main start
ret_val EQU 1
csub (2:argc,4:argv),2
pushlong argv
pushword argc
jsl foo
stz ret_val
ret 2:ret_val
end
Finally, why not call printf directly rather than calling a function
foo to do it?
// dummy
int foo (void)
{
}
mcopy main.macs
OBJCASE ON
main start
ret_val EQU 1
i EQU ret_val+2
csub (2:argc,4:argv),4
stz sum
lda #1 ;for (i=1; i <= 10; i++)
sta i
forloop ANOP
lda i
cmp #10
beq for1
bcs endfor
for1 clc
adc sum ;sum += i;
sta sum
inc i ;i++
bra forloop
endfor ANOP
pushword sum
pushlong #sumstr
pushlong #formatstr
jsl printf
stz ret_val
ret 2:ret_val
sum ds 2
sumstr dc c'sum = '
dc i1'0'
formatstr dc c'%s %d'
dc i1'13'
dc i1'0'
end
We use a dummy foo function to generate the required .root file.
Want to write a sort function in assembly but don't know how. Yes, you
can by "cheating". There are many C sort functions floating on the
Internet. Port one of them to ORCA/C. Make sure it is working properly
(as part of an ORCA/C program).
Then move the sort function to a separate source file by cutting and
pasting; compile it and use DUMPOBJ + output redirection to get a TXT
file. Edit this file by removing unwanted material and adding in
labels. Your original C function will be helpful as you peruse through
the assembly code.
Mark