The whole point of this is to put alot of concepts and ideas into one place for beginners. Strings,using strings,and reading strings are some of the things in this that were hard to find and even then were vague.I try to explain those and others with several examples of each.
These are in fact working cpu programs just copy and paste into your garrysmod/data/cpu chips folder.
Any questions on what I went over and mistakes I might've made are gladly welcome.But, try to keep the help to a level that the beginner can understand as this was what the whole thread was intended. Also note that I urge anyone to post their own programs that you know to indefinitely work in this thread. But please try to not clutter so people can easily learn from this.
This first section is writing a string to a console screen
Code:
//To wire up spawn a cpu,data port,console screen,and 2 different inputs for left and right.
//I used a 4 and 6 numpad input.Wire port 0 to whatever "left" input you have and wire port 1
//to the "right" input.Wire the cpu's membus input to the console screen.
//The whole premise is to navigate from the first screen of text to another part.Although its only 2 lines..
Start:
jmp aMain//This first section are the calls and On-screen Text.Note how each different string has a separate label i.e. Left,aStart..
text://I also skip most of this part with the "jmp aMain" since not doing so might give error if you get to the ret.
aStart://first choice that will show
db 'Welcome Player!',-1//These -1's are to show where the string(or what I db'd) stops.
db '4 is left and 6 is right',-1//Really is to let the Write call know when to stop writing.
Left://second choice
db 'Now Left.Left Again or Right?',-1//The different labels are to go to an individual string.
Right://second choice
db 'Now Right.Right Again or Left?',-1//This -1 is used again at aWrite,look.
bEnd:
db 'End of Servicable Moves',-1//Db makes the makes those words into the "string".Note that the quotes around the db have to be the same.None of this """"String'
Calls:
Clear://This type of function is one of the most useful and easy around.Let's start with the #.If eax is 50, and we tell it mov #eax,10 that will make the memory location of 50 be ten but not the eax itself.
push eax
mov eax,65536//The console screen's clear is at mem address 2041 so why are we setting it to 65536?To get on the first memory address outside of the cpu's internal ram and then to get to the screen's 2041 on its memory.
add eax,2041//We add them together to to get to the console screen's memory.2041 just happens to be where the console screen's clear screen is on its memory.
mov #eax,1//Note how we do the all-important process of clearing it with 1...
mov #eax,0//And then setting it back to avoid issues
pop eax
ret//Remember that when the code hits the "ret" it will return to whatever is underneath the call.(Continued at call clear)
Write://Ebx is the string that I am reading from,Edx is the Color of the pixel or "parameter"
mov eax,65536//If you are using hi-speed you will have to write outside of the cpu's memory,which stops at 65535.Which is why I will start at 1 above that.
aWrite:
cmp #ebx,-1//When there are still characters in the string, they won't be -1 but when the string is done the -1 will pop up.That's why I check for it.
je aEnd//If it does pop up I go ahead and jump or "jmp" to the aEnd.
mov #eax,#ebx//The way the memory goes in the con screen's memory is character then parameter then character then parameter.....
inc eax//Since we compared or "cmp"'d the #ebx,we know that it is in fact in the string so we write it to the screen's current pixel or byte which is held in the eax register
mov #eax,edx//What we moved or "mov"'d to the eax was a character on the nth pixel,it is now on the parameter or color of that same nth digit.
inc eax//increasing the eax once will go to the next pixel since we already put the character and parameter of the last one.
inc ebx//increasing this will go to the next character in the string.So if we were just at t in "The Cowboy" we are now at the h!
jmp aWrite//this will loop it back to the beginning of this function to get the whole string into the screen.Note how we jump to aWrite and not the regular Write since that would reset the eax,otherwise putting it back at the first pixel.
aEnd://Note how I never called aEnd or aWrite but just Write.It will return or "ret" to the last caller.
ret
aMain://This will start the code off with the title screen.
call clear//After the call is done the code will come back to whatever is underneath the call i.e. "mov edx,000999"
mov edx,000999//This register is for the color of the text on screena and won't be changed afterwards.
mov ebx,aStart//Note carefully about how "aStart" isn't a variable.It is where the 'Welcome Player!' string starts.
call Write//A great thing about calls is that you can do alot without having to retype it again.I use the write call in almost in the underlying labels.
aWait:
cmp port0,1//This part is just to tell if you've pressed the left or right.
je Leh
cmp port1,1
je Rih
jmp aWait//The two labels below do the same thing except say which string they want put on the screen
Leh:
call clear//This will call the clear label to "clear" the screen
mov ebx,left//I want to get the string after the left label so I set the ebx to the left label.
call write
jmp end
Rih:
call clear
mov ebx,right//They both will call clear ,then tell which string to process using ebx,then call write to have that certain string be put on-screen.
call write
End://
Buffer: //This part is highly screwed up and its purpose is to allow time for the left or right to be let go.In other words,disregard this "buffer"
cmp port0,1
je buffer
cmp port1,1
je buffer
aBuffer:
cmp port0,1
je cend
cmp port1,0
je cend
cEnd:
call clear
mov ebx,bEnd
call Write This second section is for the segment register memory opcode. Example is mov eax,es:#edi. To make it work put in the address bus settings,then wire the Memory2 to the keyboard.Then the result will be in port 1. Esp is normally moved with the push opcode but then is reverted back with the pop.
Code:
//Set the address bus to -------#1 offset 0---#2offset 2048----#1 size 2048---#2 size 64
//This program takes the first buffer of the wired keyboard to port 1.
Start:
mov es,esp//Es is being set to esp which is a stack pointer.Esp is at the bottom of the stack i.e. 65535
inc es//Since it is 1 short of 65536, we add 1 or just increment it or"inc"
mov edi,2048//This is the address where the first keyboard buffer resides
aStart:
mov eax,es:#edi// Es is the segment while edi is the general register. The address we are getting is 2048 plus 65536 which is the first keyboard buffer.
out 1,eax//We moved Eax to the above address so we could take that to port 1.
jmp aStart//Note that I jumped to aStart and not Start as doing that would inc es again.
This third segment is on keyboard to console functions. Note this is a very slow way of doing it if you type slow. It's weird like that.
Set the address bus to first offset at 0 then second offset at 2048. Then first size at 2048 then second size to 64. Then wire mem1 to console then mem2 to keyboard.
Code:
Entry:
jmp SetMainRegs
alloc CharPos
define Buffer,2048//The first byte on the keyboard aka "The buffer"
define Char,2049//The first actual character on the keyboard
define Color,000999
Calls:
GetKey://Returned in edx
push eax
mov eax,Char
add eax,es
out 0,#eax
mov edx,#eax
pop eax
ret
ResBuffer:
push eax;//This is to reset the first "buffer" or byte on the keyboard.
mov eax,Buffer
add eax,es;mov #eax,-1//Setting it to -1 seems to be the only reliable way to reset it.
pop eax
ret
WriteChar:
call Getkey
push esi
mov esi,#CharPos
add esi,es
mov #esi,edx//The Character has been set
inc esi
mov #esi,Color
add #CharPos,2
pop esi
ret
KeyDown:
push esi//Really just a check
mov esi,Buffer// We set esi to the buffer
add esi,es// then add esi to es to get the actual buffer location
mov eax,esi// then we set eax to whatever the buffer is to free esi
pop esi
aKeyDown:
cmp #eax,0
je aKeyDown
call Resbuffer
call WriteChar
ret
SetMainRegs:// I will use es to get directly out the cpu's memory
mov es,esp// esp starts at 65535 which is one less than we want so....
inc es
Start://Main functions
call KeyDown
jmp Start
Bookmarks