masm - How can I reverse and modify my string in assembly? -
i have project want enter number , enter 3, gives output of,
zyx**xyz zy****yz z******z
and 5 give you
zyxwv**vwxyz zyxw****wxyz zyx******xyz zy********yz z**********z
in project don't think instructor allow me use array, or @ least not yet here idea.
i thinking of making string number 3. produce zyx* , reverse other half of triangle. thing is, don't know how change letters 1 @ time stars. i'm thinking of using loops not sure how it. know next string zy** , reverse it.
don't me wrong, i'm not asking me maybe give me pointers or tips on how approach it. thank you.
so far, all, have been able come this.
title masm template (main.asm) ; description: ; ; revision date: include irvine32.inc .data x dword ? msg byte "please input number: " ,0dh,0ah,0 .code ;crlf main proc call clrscr mov edx, offset msg ; moves message input number register call writestring ; displays message on screen prompt user input number call readint ; take number user inputs mov x,eax ; store x mov ecx, eax ; loop counter mov al, 'z' ; move z register l2: mov al, 'z' ; resets al z loop l1: ; start of loop label l1 call writechar ; write letters ;call crlf ; put in 'enter' sub al, 1 ; move next char going downward loop l1 mov al, ' ' call writechar mov ecx, x ; resets ecx outside loop sub x, 1 ; decrements x counter call crlf ; tidy loop l2 exit main endp end main
now need other side.
welcome blackest of black computer arts - assembler!
let's @ problem assembler programmer's point of view.
you have number, let's bl ('cause that's assemblerish name) , want produce bl lines of output. let's bl=5
now, cx counter register, if copy bl cl , clear ch xoring itself, have cx=#lines too.
the number of characters have print before reversing conveniently 1 more this, increment cx - , that's important number, save in say, bp
each line consists of letters 'z' down ('z' - bh + 1) , 1 asterisk, same in reverse, , new line. , every new line, reduce number of letters printed 1 , add 1 number of asterisks.
to produce 1 line, we'd
- load register - say, dl, 'z' (dl because it's convenient output 'printme' routine)
- copy register, bh (a register-to-register move faster , smaller load-immediate)
- subtract bl bh, yielding magic value, 'z' - 5 = 'u' - there bell ringing yet?
we want print count of letters, if copy bp cx, should useful.
here's want cx times:
- write out character in dl - twist.
- change character in dl decrementing it
then need output bp bytes, sopy bp cx again
here's want cx times:
- write out character in dl - twist.
- change character in dl incrementing it
then send cr , lf
so - what's twist?
well, if character in dl less or equal character in bh, want substitute asterisk, so
- save dx on stack
- load dl asterisk if dl<=bh
- output dl
- pop dx stack, restoring value before substituted asterisk.
done line - decrement count of lines in bl; if result non-zero, produce line
- if zero, we're done.
note here decrementing count of lines in bl, number subtracted dl @ start of produce-a-line routine decrements, 'change aster' value becomes 'v', 'w'...
now let's downright kinky.
you'll note difference between 2 write-cx-characters routines 1 decrements , other increments dl...
let's set si 0ffffh , di 1 before start, laughs.
now - dh sitting on bench entire game.
suppose write routine print cx characters such adds si dx. give decrement need in loop first time.
- when loop ends, suppose exchange di , si.
- the next time loop runs, increment dx, exchange si , di again.
useful, huh?
oh, , while we're twisting, can dh involved using store dl rather pushing dx , popping back. move dl dh, test substituting aster, write out , restore dl dh.
so - point routine doesn't rely on string in storage...
Comments
Post a Comment