These are to study for a test from, so they must be well commented.
Also, if you cannot answer all 4, please let me know which problems
you could solve, and I will set a new question up with a new price for
you. Thanks !
1.
Random Strings
Write a program that generates and displays twenty random strings,
each consiting of ten capital letters {A...Z}.
2.
Color Matrix
Write a program that displays a single character in all possible
combinations of foreground and background colors (16x16 =256). The
colors are numbered from 0 to 15, so you can use a nested loop to
generate all possible combinations.
3. ArrayScan using LOOPZ
Using the ArrayScan program below, implement the search using the
LOOPZ instruction.
TITLE Scanning an Array
;Scan an array for the first nonzero value.
INCLUDE Irvine32.inc
.data
intArray SWORD 0,0,0,0,1,20,35,-12,66,4,0
;intArray SWORD 1,0,0,0 ;alternate test data
;intArray SWORD 0,0,0,0 ;alternate test data
;intArray SWORD 0,0,0,1 ;alternate test data
noneMsg BYTE "A non-zero value was not found",0
.code
main PROC
mov ebx,OFFSET intArray ;point to the array
mov ecx,LENGTHOF intArray ;loop counter
L1:
cmp WORD PTR [ebx],0 ;compare a value to zero
jnz found ;found a value
add ebx,2 ;point to next
loop L1 ;contine the loop
jmp notFound ;none found
found: ;display the value
movsx eax,WORD PTR[ebx]
call WriteInt
jmp quit
notFound: ;display "not found" message
mov edx,OFFSET noneMsg
call WriteString
quit:
call crlf
exit
main ENDP
END main
4. Greatest Common Divisor (GCD)
The greatest common divisor of two integers is the largest integer
that will evenly divide both integers. The GCD algorithm involves
integer division in a loop, described by the following C++ code:
int GCD(int x, int y)
{
x = abs(x);
y = abs(y);
do {
int n = x % y;
x = y;
y = n;
} while y > 0;
return x;
}
Implement this function in assembly language and write a test program
that calls the function several times, passing it different values.
Display all results on the screen. |
Request for Question Clarification by
dogbite-ga
on
01 May 2003 13:48 PDT
Hello bildy-ga,
I'd be happy to write the programs
if it's in one of the languages that
I'm familiar with. What instruction
set do you need it written for?
dogbite-ga
|
Clarification of Question by
bildy-ga
on
01 May 2003 14:48 PDT
I need these 4 programs written in assembler which can be compiled in
MASM for windows. I also need these programs written as simple as
possible, as I am using them to study for a test for a intro level
assembly programming class. Please post any further needs for
clarification.
Thanks!
|
Request for Question Clarification by
dogbite-ga
on
02 May 2003 10:36 PDT
I'm sorry -- I do not have enough familiarity
with MASM to help. Maybe another research can help.
dogbite-ga
|
Clarification of Question by
bildy-ga
on
02 May 2003 16:47 PDT
What school are you a TA at. Thats cool, but my school doesn thave
TA's. Anyways, I just need help with these questions to study for a
test from, thats all, I just wanna pass my final.
|
Request for Question Clarification by
mathtalk-ga
on
03 May 2003 11:56 PDT
Hi, bildy-ga:
From the nature of some of these programs, I would suspect that it
would be simplest to create them as DOS programs. However you have
"clarified" that you want them to be created using MASM to run in
Windows. What version of Windows do you have in mind? Have you been
given a "shell" application that runs (perhaps from a command line) in
Windows? Does a program have to be in "New EXE" format, or are older
executable file formats acceptable?
regards, mathtalk
|
Clarification of Question by
bildy-ga
on
03 May 2003 12:41 PDT
That is exactly what I meant..programs that compile in MASM, and are
DOS based. Are you able to complete these programs, I need these as
soon as possible, as the test date is fast approaching....
|
Request for Question Clarification by
mathtalk-ga
on
03 May 2003 15:10 PDT
Hi, bildy-ga:
I have MASM and can certainly write and test the sorts of programs you
asked about. I'm concerned that I may not be able to accomplish all
that you ask in a short time frame, esp. given the price offered.
Google Answers has some guidelines on pricing here:
http://answers.google.com/answers/pricing.html
Multi-part questions are normally priced at $50 and above.
I'm inclined to think of writing and testing any single program as a
multi-part question. However let me look around. Possibly I can find
examples for three of the four programs and only write one from
scratch. I'll let you know what I've found in about six hours.
regards, mathtalk-ga
|
Request for Question Clarification by
mathtalk-ga
on
03 May 2003 21:13 PDT
Hi, bildy-ga:
From looking at the sample code you showed in connection with program
3, it seems that your coursework may be based on this book:
Kip R. Irvine, Assembly Language for Intel-based Computers
currently in its fourth edition by Prentice-Hall (1999).
The main purposes of irvine32.inc and irvine32.lib are to provide for
"hiding" of some details of the memory model and for supplying
simplified "console" IO in the extended DOS environment supported by
32-bit Windows. [There are corresponding files irvine16.inc and
irvine16.lib which support "real mode" DOS programming.]
There are a number of sample programs around the Web. If you are
intent on getting the four programs listed above done from scratch, it
will mean considerable effort. If this were being done over (say) a
week's timeframe, then this might not be an insuperable obstacle. But
I have the feeling that you may well want these programs available for
study as soon as possible.
Please clarify whether precisely these exact four programs are needed,
or if some similar but not identical functionality would be
acceptable. Assuming that you would like exactly the specified
functionality, with extensive comments, and that speedy answers are
essential, then I'd recommend posting the four programs as separate
questions in order to get as many researchers working on them as
possible.
I would at best be able to deliver one program a day, assuming no
major interruptions. Moreover I'd be reluctant to post any one
program in advance of having all four of them written (for a question
in which you've intentionally combined all four of them from the
outset).
So again it seems to me in your interests to separate them out, even
though I'm not sure how many researchers will have a copy of MASM at
their disposal. The four programs, it seems to me, will vary
considerably in difficulty and length.
best wishes, mathtalk-ga
|
Clarification of Question by
bildy-ga
on
04 May 2003 09:15 PDT
If they are close enough to answer the question, then by all means
please post them. I am looking forward to seeing what you come up
with. Thanks Alot !
|
Clarification of Question by
bildy-ga
on
04 May 2003 14:06 PDT
MathTalk:
If at all possible, could you please post these programs as soon as
possible. Thanks...
|
Request for Question Clarification by
mathtalk-ga
on
04 May 2003 19:39 PDT
Hi, bildy-ga:
There's a listing of a sample program using irvine32.inc and
irvine32.lib here:
http://zeus.cs.pacificu.edu/ryand/cs320/ch5a03.html
taken from Kip Irvine's book (Assembly Language for Intel-based
Computers).
If you haven't tried to assemble and link this program, I suggest you
start there. The program has calls to the Randomize and SetTextColor
functions in the link library written by Irvine, functions which are
certainly germane to your programming requests 1 and 2.
Programming request 3 below is fairly self-contained; if I understand
it, one is merely asked to rewrite the "JNZ...LOOP" construct to use a
LOOPZ instruction instead (making the search more efficient).
A program that matches your programming request 4 quite closely can be
found here:
http://ihower.idv.tw/hower/4/ASM/hw3.txt
I know you are anxious to have something to use in your study review
as quickly as possible, so I'm bending the rules a bit here to post a
substantial part of my information here, rather than as an answer. My
thought is to proceed with the modification needed for programming
request 3, with detailed comments to explain the difference in looping
instructions.
Please look these links over and let me know whether you agree with
this approach/priority.
regards, mathtalk-ga
|
Clarification of Question by
bildy-ga
on
05 May 2003 14:46 PDT
Please provide the modified programs in the form of an answer as soon
as possible. Thanks.
|
Request for Question Clarification by
mathtalk-ga
on
05 May 2003 19:05 PDT
Hi, bildy-ga:
I'm concerned about how these last few exchanges of posts have
unfolded. I've provided some fairly detailed information, and asked
about some specifics in terms of how to proceed that are not clearly
addressed in your last few short posts. Perhaps we are failing to
communicate?
What is coming through is that you want answers fast (although no
specific deadline is mentioned), and I have the impression that you
still expect four fully commented assembly language programs written
to your specifications.
I don't think I would be able to provide all four such programs in the
short but unspecified timeframe that you require. Any researcher that
may be able to help you would be greatly assisted in making such a
determination by your clarifying how much and by when you expect an
answer, esp. given the current list price.
regards, mathtalk-ga
|
Request for Question Clarification by
studboy-ga
on
09 May 2003 15:32 PDT
mathtalk-ga is right--you need to be more specific. I do have *your*
version of MASM and can help you test out the programs, but you need
to be more specific.
Also, it helps if the fee is more in line with the assignment--$25 per
program is the going rate I believe. Thanks.
|
Clarification of Question by
bildy-ga
on
09 May 2003 19:47 PDT
I am adding one more problem, but upping the amount to $100.
5. Exchanging Integers
Create an array of randomly ordered integers. Using the Swap
procedure as a tool, write a loop that exchanges each consecutive pair
of integers in the array. Use a display function to print out to the
display a pre-contents before the swap and a post-contents after the
swap.
I need these programs by Monday. Please let me know if this is
acceptable. thanks...
|
Request for Question Clarification by
mathtalk-ga
on
09 May 2003 20:07 PDT
Hi, bildy-ga:
Thanks for clarifying that you want five programs written by Monday.
I would not be able to do a thorough job for you under these
constraints. Perhaps studboy-ga or another researcher will be able to
assist you in the given timeframe.
best wishes, mathtalk-ga
|
Clarification of Question by
bildy-ga
on
09 May 2003 23:38 PDT
Which problems could you complete? By when? and how much?
Thanks
|
Request for Question Clarification by
mathtalk-ga
on
10 May 2003 01:30 PDT
Hi, bildy-ga:
I don't know if you read my post from a few days back, where I
provided some links to a couple of program samples: one from the
"book" that is related to your programs 1,2 and another which
implements the GCD algortithm of your program 4. I offered to get
started on program 3, which is a straightforward modification to the
given code in your question (using LOOPZ rather than a conditional
jump plus LOOP instruction combination).
If you haven't closely review those materials, I recommend you do so
now. I think they will go a long way toward helping you prepare for
the final exam. Since you've mostly finished the course, I was hoping
to hear some detailed feedback from you on whether those materials are
useful or not, e.g. because they are not commented well enough or they
are too far from what you wanted the programs to do.
I threw out the idea that I could probably do one program a day. I
might be pretty conservative in terms of what others might be able to
get done, but I do test my code pretty thoroughly before I put it out
in an answer. Even though you are planning to _study_ the code rather
than use it to (say) run a nuclear reactor, I think I'd be most
comfortable with my usual level of testing.
Since studboy-ga mentioned that he has MASM, that might give you an
additional option.
Why don't you take a look at the sample programs linked above, see how
well they serve your purpose (if at all), and then prioritize what you
want done by Monday. I can't really commit to doing more than two of
the programs for you (and now there are five that you've put on the
table), so your input on the priorities is a key here.
regards, mathtalk-ga
|
Clarification of Question by
bildy-ga
on
10 May 2003 08:21 PDT
Ok after reviewing all of the material . . . Could you rewrite the
code so that questions 1 and 2 work exactly as specified. The program
provided is way too far off. I will pay $25 for each program for
these two. If you could provide them before Monday that would be
great, but Monday is acceptable. I will post further questions on
google and let you know through a comment where they are located.
Thanks !
|
Clarification of Question by
bildy-ga
on
12 May 2003 13:33 PDT
will they be completed by tomorrow?
|
Request for Question Clarification by
mathtalk-ga
on
12 May 2003 14:28 PDT
Hi, bildy-ga:
Yes, they will be completed tonight. I've been installing the latest
version of MASM (version 6.15) just for the sake of full compatibility
with the Irvine book. Surprisingly this latest version is included
with VS.Net 2003, and with VS6 (in the form of a "Processor Pack") but
not the "in-between" VS.Net 2002.
regards, mathtalk-ga
|
Clarification of Question by
bildy-ga
on
12 May 2003 19:27 PDT
How are these 2 programs coming along?
|
Hi, bildy-ga:
Here's the way I build the program; your set up may be a little
different, but I think the variations should be minor.
There's usually a .BAT file that is run to set certain "DOS"
environment variables. In my case I'm using the MASM 6.15 tools from
my VS.Net 2003 installation, which creates a menu item:
Visual Studio .NET 2003 Command Prompt
which runs just such a .BAT file behind the scenes before opening a
"console" box.
I've placed the .ASM source code I wrote in a common directory with
three files from Kip Irvine's textbook/CD:
irvine32.inc
SmallWin.inc
irvine32.lib
After navigating into this directory, where the source code for the
first program is called RandmStr.ASM, I can use this command:
ml RandmStr.ASM /link irvine32.lib kernel32.lib /SUBSYSTEM:CONSOLE
If you have any questions about adapting the build to your own setup,
please ask for a clarification. In the meantime, here's the code for
the first program. I think it's laid out so that no errant
line-breaks crop up:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
; Random Strings
; Write a program that generates and displays twenty random strings,
; each consisting of ten capital letters {A...Z}.
; Last update: 05/13/03
INCLUDE Irvine32.inc
CR = 0Dh ; carriage return
LF = 0Ah ; line feed
.data
rand1 BYTE "Generating 20 random strings",CR,LF,0
.code
main PROC
; Set text color to white text on black background
; even though these are the default colors.
; Each color constant is defined in Irvine32.inc
mov eax,white + (black * 16)
call SetTextColor
call Clrscr ; clear the screen
call Randomize ; reset random number sequence
; Generate and display 20 random strings,
; only put a half second delay between each.
mov edx,OFFSET rand1 ; display message
call WriteString
mov ecx,20 ; outer loop counter
L1: push ecx
call myRandomString ; display random string
mov eax,500
call Delay ; pause for 500 milliseconds
pop ecx
Loop L1
; Okay, we've displayed all 20 random strings.
; Get an ENTER keypress from the user; this
; let's the results remain visible.
call Crlf ; new line
call WaitMsg ; "Press [Enter]..."
; Exiting the program invokes standard console epilog.
exit
main ENDP
myRandomString PROC
; ASCII values for capital letters A-Z are 65 to 90.
; Note that 65 must be added to range 0 to 25.
; Set up to loop 10 times getting random characters
mov ecx,10 ; inner loop counter
L2: mov eax,26
call RandomRange ; EAX = 0 to 25
add eax,65
; We need to display this character:
call WriteChar
Loop L2
; Eventually the loop ends, all 10 characters shown.
mov eax,CR
call WriteChar
mov eax,LF
call WriteChar
; Return from subroutine:
ret
myRandomString ENDP
END main |
Request for Answer Clarification by
bildy-ga
on
13 May 2003 13:14 PDT
That is only question #1, we agreed upon you answering the first 2
questions. I will test this code out as soon as I get home and let
you know how I make out. . .
|
Clarification of Answer by
mathtalk-ga
on
13 May 2003 14:08 PDT
The second program is in the same directory and named ColorMtx.ASM, so
to build it you only need to change the source filename in the
command:
ml ColorMtx.ASM /link irvine32.lib kernel32.lib /SUBSYSTEM:CONSOLE
regards, mathtalk-ga
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
; Color Matrix
; Write a program that displays a single character in all possible
; combinations of foreground and background colors (16x16 =256). The
; colors are numbered from 0 to 15, so you can use a nested loop to
; generate all possible combinations.
; Last update: 05/13/03
INCLUDE Irvine32.inc
CR = 0Dh ; carriage return
LF = 0Ah ; line feed
.data
prompt1 BYTE "Please type a character: ",0
dispChr DWORD ?
.code
main PROC
; Set text color to white text on black background
; even though these are the default colors.
; Each color constant is defined in Irvine32.inc
mov eax,white + (black * 16)
call SetTextColor
call Clrscr ; clear the screen
; Get the user to type some character for our display:
mov edx,OFFSET prompt1 ; "Please type a..."
call WriteString
call ReadChar
; One now has the desired character in AL, but we can
; only use it later, so we save it in variable dispChr:
mov dispChr,eax
call Crlf ; new line
; Generating all the possible color combinations in a nested
; loop requires a bit more work than simply a single loop.
; ECX will be preserved on the stack across the inner loop:
mov ecx,16 ; outer loop counter
L1: push ecx ; save outer loop counter
; As the text attributes above illustrate, to set colors we
; need to compute EAX in a way that combines both attributes.
; Set EAX using the stack copy of ECX value so the background
; will vary slowest (with the outer loop counter).
mov ecx,16 ; inner loop counter
L2: pop eax ; gets the outer loop counter in EAX
push eax ; saves it back again
sub eax,1 ; shifts range from 1-16 to 0-15
rol eax,4 ; sets AL for the background color
add eax,ecx ; adds the inner loop counter of ECX
sub eax,1 ; sets AL for the foreground color
call SetTextColor
; EAX is now available for restoring the display character
mov eax,dispChr
call WriteChar
Loop L2
; Return the cursor to beginning of next row for matrix
call Crlf ; new line
pop ecx ; restore outer loop counter
Loop L1
; Show the results on screen until user hits enter, then exit.
mov eax,white + (black * 16)
call SetTextColor ; otherwise we leave black on black
call Crlf ; new line
call WaitMsg ; "Press [Enter]..."
exit
main ENDP
END main
|
Clarification of Answer by
mathtalk-ga
on
13 May 2003 15:13 PDT
Hi, bildy-ga:
Since I'd already done more work on the third program upfront than on
the first two programs, I'm throwing the third program in as a bonus.
Replacing the LOOP instruction with LOOPZ allows us to save an
instruction inside the loop, namely the jump instruction that would
take us out of the loop if a nonzero entry were found. However in
order to accomodate this genuine optimization, we need to restructure
the entry and exit from the loop.
We want the LOOPZ instruction, which by definition has to be last in
the loop, to see the zero flag as it is set by the compare
instruction, and not as it might be set by the incrementation of the
pointer into the array. We still need the pointer incrementation
though, so it has to be moved to the beginning of the loop (where it
will not affect the zero flag as LOOPZ sees it).
But then we have to fiddle a bit with the way we first enter the loop.
We want to start with EBX pointing to the beginning of the array, but
if we execute the increment to EBX first, then we would skip that
entry. There are two or three ways to work around this, but I think
the way shown below is the most consistent with this exercise. We
jump over that first instruction in the loop when we enter the loop
the first time.
As far as restructuring the loop's exit, notice that originally
"falling through" the loop meant that we'd reached the end of the
array (ECX = 0), so that guaranteed us no nonzero value was
encountered. Here, using the LOOPZ instruction, we wind up in the
same "program location" either if a nonzero value is found or if we
simply searched all the way to the end of the array without finding
such a value.
So it's necessary to test the zero flag one more time. Fortunately
the LOOPZ instruction doesn't affect the zero flag, so we are still in
position to use a JNZ (jump if nonzero) instruction to see whether or
not we reached the end of the array without success. It's a little
confusing, but the zero flag _not_ being set means we were successful
in finding a nonzero entry. The zero flag being set would mean every
comparison to an array entry resulted in a zero.
The two "outcomes" found and not found have been accordingly reversed
(although if one prefers, a JZ = jump if equal instruction could have
been used to keep these final labelled branches in the original
order).
regards, mathtalk-ga
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
; Scan an array for the first nonzero value.
; Modified 05/13/03 to use LOOPZ instruction
INCLUDE Irvine32.inc
.data
intArray SWORD 0,0,0,0,1,20,35,-12,66,4,0
;intArray SWORD 1,0,0,0 ;alternate test data
;intArray SWORD 0,0,0,0 ;alternate test data
;intArray SWORD 0,0,0,1 ;alternate test data
noneMsg BYTE "A non-zero value was not found",0
.code
main PROC
mov ebx,OFFSET intArray ; point to the array
mov ecx,LENGTHOF intArray ; loop counter
; Jump into middle of loop, past where EBX is incremented
jmp L2
; Increment of EBX moved here so it won't affect zero flag
L1: add ebx,2
; Compare sets zero flag if both values are equal
L2: cmp WORD PTR [ebx],0 ; compares entry to zero
; If array entry is zero and ECX > 0, continue. We reach the end
; of the array when ECX = 0.
loopz L1
; LOOPx instructions don't affect the zero flag, so determine
; if we exited the loop because a nonzero entry was found by
; checking the zero flag one last time:
jnz found
; Otherwise display "not found" message, loop ended with ECX = 0
notFound:
mov edx,OFFSET noneMsg
call WriteString
jmp quit
found: ; display the value
movsx eax,WORD PTR[ebx]
call WriteInt
quit:
call Crlf
exit
main ENDP
END main
|
Request for Answer Clarification by
bildy-ga
on
13 May 2003 20:09 PDT
I have yet to check these problems as I am still stuck at work, but I
would ask if you could just take a minute to correct this single piece
of code that I have written.
QUESTION:
Write a program using the LOOP instruction with indirect addressing
that copies a string from source to target, reversing the character
order in the process. Use the following variables:
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP(0)
Insert the following statements immediately after the loop. They will
display the hexadecimal contents of the target string:
mov esi,OFFSET target ;offset of variable
mov ebx,1 ;byte format
mov ecx,SIZEOF target-1 ;counter
call DumpMem
If your program works correctly, you will see the following sequence
of hexadecimal bytes on the screen when the program runs:
67 6E 69 72 74 73 20 65 63 72 75 6F 73 20 65 68
74 20 73 69 20 73 69 68 54
HERE IS MY CODE:
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string",0
nameSize = ($ - source) - 1
target BYTE SIZEOF source DUP(0)
.code
main PROC
; Push the name on the stack.
mov ecx,nameSize
mov esi,0
L1: movzx eax,source[esi] ; get character
push eax ; push on stack
inc esi
Loop L1
; Pop the name from the stack, in reverse
; store in the source array.
mov ecx,nameSize
mov esi,0
L2: pop eax ; get character
mov source[esi],al ; store in string
inc esi
Loop L2
mov esi, OFFSET target
mov ebx,1
mov ecx,SIZEOF target-1
call DumpMem ; Display the name.
mov edx,OFFSET source
call Writestring
call Crlf
exit
main ENDP
END main
MY PROBLEM:
The string reverses itself perfectly, but the hexadecimal output is as
follows:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00
A generous tip will be given if you decide to help me out with this
little problem and once I test your previous code . . . .
|
Clarification of Answer by
mathtalk-ga
on
13 May 2003 22:02 PDT
Hi, bildy-ga:
The main problem with your code is that you are popping the source
string off the stack back into the location of source. What you meant
to do (I think) is to pop it off into the location of target.
The reason you thought that the code was working is that you made a
counteracting mistake in writing out the source string (rather than
the target string) at the end of the program.
regards, mathtalk-ga
|