So I did some assembly programming:

Place to talk about all that new hardware and decaying software you have.

Moderator: General Mods

Post Reply
Haneda
-Burninated-
Posts: 33
Joined: Thu Feb 26, 2009 8:55 pm
Location: Zanzibar Land

So I did some assembly programming:

Post by Haneda »

Granted, not for any particular processor. The assembler I used today in college is called "AsmTutor", which emulates an imaginary (non-existent) processor and allows you to write assembly code for it. It's not that complex of a machine, only having 8 registers and all. It's instruction set is quite simple, given that it's only a teaching/learning assembly platform.

Here are some of the (very simple) programs that I wrote:

Code: Select all

0
200
0 0			<reserved>
1 0		0	keyboard interrupt service address
2 0			<reserved>
3 0			<reserved>
4 0			keyboard buffer location
5 0			mapped memory
6 0			mapped memory
7 0			mapped memory
8 0			mapped memory
9 0			mapped memory
10 0			mapped memory
11 0			mapped memory
12 0			mapped memory
13 0			mapped memory
14 0			mapped memory
15 0		0	stack area - program return address
16 0			stack area
17 0			stack area
18 0			stack area
19 0			stack area
20 0			stack area
21 0			stack area
22 0			stack area
23 0			stack area
24 0			stack area
25 0			stack area
26 0			stack area
27 0			stack area
28 0			stack area
29 0			stack area
30 0			stack area
31 0			stack area
32 0			stack area
33 0			stack area
34 0			stack area
35 0			stack area
36 0			stack area
37 0			stack area
38 0			stack area
39 0			stack area
40 0			stack area
41 0			stack area
42 0			stack area
43 0			stack area
44 0			stack area
45 0			stack area
46 0			stack area
47 0			stack area
48 0			stack area
49 0			stack area
50 0			stack area
51 0			stack area
52 0			stack area
53 0			stack area
54 0			stack area
55 0			stack area
56 0			stack area
57 0			stack area
58 0			stack area
59 0			stack area
60 0			stack area
61 0			stack area
62 0			stack area
63 0			stack area
64 0			stack area
65 0			stack area
66 0			stack area
67 0			stack area
68 0			stack area
69 0			stack area
70 0			stack area
71 0			stack area
72 0			stack area
73 0			stack area
74 0			stack area
75 0			stack area
76 0			stack area
77 0			stack area
78 0			stack area
79 0			stack area
80 0			stack area
81 0			stack area
82 0			stack area
83 0			stack area
84 0			stack area
85 0			stack area
86 0	RTS		print string routine - R0=string address
87 0	RTS		print number routine - R0=number
88 0	RTS		get string routine - R0=string buffer, R1=buffer size
89 0	RTS		get number routine - returns number in R0
90 0			<reserved>
91 0			<reserved>
92 0			<reserved>
93 0			<reserved>
94 0			<reserved>
95 0			<reserved>
96 0			<reserved>
97 0			<reserved>
98 0			<reserved>
99 0			<reserved>
100 0	move	4,r0	;read keyboard input
101 0	move	r0,10	;move contents of r0 to memory address 10
102 0	cmp	#'Q',r0	;does r0 contain the character Q?
103 0	bne	100	;if no contain Q, goto memory address 100
104 0	rts		

Code: Select all

0
200
0 0			<reserved>
1 0		0	keyboard interrupt service address
2 0			<reserved>
3 0			<reserved>
4 0			keyboard buffer location
5 0			mapped memory
6 0			mapped memory
7 0			mapped memory
8 0			mapped memory
9 0			mapped memory
10 0			mapped memory
11 0			mapped memory
12 0			mapped memory
13 0			mapped memory
14 0			mapped memory
15 0		0	stack area - program return address
16 0			stack area
17 0			stack area
18 0			stack area
19 0			stack area
20 0			stack area
21 0			stack area
22 0			stack area
23 0			stack area
24 0			stack area
25 0			stack area
26 0			stack area
27 0			stack area
28 0			stack area
29 0			stack area
30 0			stack area
31 0			stack area
32 0			stack area
33 0			stack area
34 0			stack area
35 0			stack area
36 0			stack area
37 0			stack area
38 0			stack area
39 0			stack area
40 0			stack area
41 0			stack area
42 0			stack area
43 0			stack area
44 0			stack area
45 0			stack area
46 0			stack area
47 0			stack area
48 0			stack area
49 0			stack area
50 0			stack area
51 0			stack area
52 0			stack area
53 0			stack area
54 0			stack area
55 0			stack area
56 0			stack area
57 0			stack area
58 0			stack area
59 0			stack area
60 0			stack area
61 0			stack area
62 0			stack area
63 0			stack area
64 0			stack area
65 0			stack area
66 0			stack area
67 0			stack area
68 0			stack area
69 0			stack area
70 0			stack area
71 0			stack area
72 0			stack area
73 0			stack area
74 0			stack area
75 0			stack area
76 0			stack area
77 0			stack area
78 0			stack area
79 0			stack area
80 0			stack area
81 0			stack area
82 0			stack area
83 0			stack area
84 0			stack area
85 0			stack area
86 0	RTS		print string routine - R0=string address
87 0	RTS		print number routine - R0=number
88 0	RTS		get string routine - R0=string buffer, R1=buffer size
89 0	RTS		get number routine - returns number in R0
90 0			<reserved>
91 0			<reserved>
92 0			<reserved>
93 0			<reserved>
94 0			<reserved>
95 0			<reserved>
96 0			<reserved>
97 0			<reserved>
98 0			<reserved>
99 0			<reserved>
100 0	move	#0,r0	;move 0 to r0
101 0	add	#1,r0	;add 1 to r0
102 0	cmp	#6,r0	;is r0 = 6 ?
103 0	bne	101	;branch if not equal to 101 (i.e. if #6 != r0 then goto location 101)
104 0	rts		

Code: Select all

0
200
0 0			<reserved>
1 0		0	keyboard interrupt service address
2 0			<reserved>
3 0			<reserved>
4 0			keyboard buffer location
5 0			mapped memory
6 0			mapped memory
7 0			mapped memory
8 0			mapped memory
9 0			mapped memory
10 0			mapped memory
11 0			mapped memory
12 0			mapped memory
13 0			mapped memory
14 0			mapped memory
15 0		0	stack area - program return address
16 0			stack area
17 0			stack area
18 0			stack area
19 0			stack area
20 0			stack area
21 0			stack area
22 0			stack area
23 0			stack area
24 0			stack area
25 0			stack area
26 0			stack area
27 0			stack area
28 0			stack area
29 0			stack area
30 0			stack area
31 0			stack area
32 0			stack area
33 0			stack area
34 0			stack area
35 0			stack area
36 0			stack area
37 0			stack area
38 0			stack area
39 0			stack area
40 0			stack area
41 0			stack area
42 0			stack area
43 0			stack area
44 0			stack area
45 0			stack area
46 0			stack area
47 0			stack area
48 0			stack area
49 0			stack area
50 0			stack area
51 0			stack area
52 0			stack area
53 0			stack area
54 0			stack area
55 0			stack area
56 0			stack area
57 0			stack area
58 0			stack area
59 0			stack area
60 0			stack area
61 0			stack area
62 0			stack area
63 0			stack area
64 0			stack area
65 0			stack area
66 0			stack area
67 0			stack area
68 0			stack area
69 0			stack area
70 0			stack area
71 0			stack area
72 0			stack area
73 0			stack area
74 0			stack area
75 0			stack area
76 0			stack area
77 0			stack area
78 0			stack area
79 0			stack area
80 0			stack area
81 0			stack area
82 0			stack area
83 0			stack area
84 0			stack area
85 0			stack area
86 0	RTS		print string routine - R0=string address
87 0	RTS		print number routine - R0=number
88 0	RTS		get string routine - R0=string buffer, R1=buffer size
89 0	RTS		get number routine - returns number in R0
90 0			<reserved>
91 0			<reserved>
92 0			<reserved>
93 0			<reserved>
94 0			<reserved>
95 0			<reserved>
96 0			<reserved>
97 0			<reserved>
98 0			<reserved>
99 0			<reserved>
100 0	move	#116,r0	move contents of memory address 116 to register 0
101 0	cmp	#0,r0	is r0 equal to 0?
102 0	beq	105	if r0 = 0 then goto memory address 105
103 0	sub	#1,r0	subtract value 1 from register 0
104 0	jmp	101	jump to location 101
105 0	rts		

Code: Select all

0
200
0 0			<reserved>
1 0		0	keyboard interrupt service address
2 0			<reserved>
3 0			<reserved>
4 0			keyboard buffer location
5 0			mapped memory
6 0			mapped memory
7 0			mapped memory
8 0			mapped memory
9 0			mapped memory
10 0			mapped memory
11 0			mapped memory
12 0			mapped memory
13 0			mapped memory
14 0			mapped memory
15 0		0	stack area - program return address
16 0			stack area
17 0			stack area
18 0			stack area
19 0			stack area
20 0			stack area
21 0			stack area
22 0			stack area
23 0			stack area
24 0			stack area
25 0			stack area
26 0			stack area
27 0			stack area
28 0			stack area
29 0			stack area
30 0			stack area
31 0			stack area
32 0			stack area
33 0			stack area
34 0			stack area
35 0			stack area
36 0			stack area
37 0			stack area
38 0			stack area
39 0			stack area
40 0			stack area
41 0			stack area
42 0			stack area
43 0			stack area
44 0			stack area
45 0			stack area
46 0			stack area
47 0			stack area
48 0			stack area
49 0			stack area
50 0			stack area
51 0			stack area
52 0			stack area
53 0			stack area
54 0			stack area
55 0			stack area
56 0			stack area
57 0			stack area
58 0			stack area
59 0			stack area
60 0			stack area
61 0			stack area
62 0			stack area
63 0			stack area
64 0			stack area
65 0			stack area
66 0			stack area
67 0			stack area
68 0			stack area
69 0			stack area
70 0			stack area
71 0			stack area
72 0			stack area
73 0			stack area
74 0			stack area
75 0			stack area
76 0			stack area
77 0			stack area
78 0			stack area
79 0			stack area
80 0			stack area
81 0			stack area
82 0			stack area
83 0			stack area
84 0			stack area
85 0			stack area
86 0	RTS		print string routine - R0=string address
87 0	RTS		print number routine - R0=number
88 0	RTS		get string routine - R0=string buffer, R1=buffer size
89 0	RTS		get number routine - returns number in R0
90 0			<reserved>
91 0			<reserved>
92 0			<reserved>
93 0			<reserved>
94 0			<reserved>
95 0			<reserved>
96 0			<reserved>
97 0			<reserved>
98 0			<reserved>
99 0			<reserved>
100 0	move	#1,r0	<--- START YOUR PROGRAM HERE
101 0	move	#1,r1	
102 0	cmp	#3,r1	
103 0	beq	106	
104 0	add	#1,r1	
105 0	jmp	102	
106 0	cmp	#2,r0	
107 0	beq	110	
108 0	add	#1,r0	
109 0	jmp	102	
110 0	rts		
112 0	add	r3,r2	
113 0	add	r4,r2	
What I am told is an example of how strings actually work in high-level languages:

Code: Select all

0
200
0 0			<reserved>
1 0		0	keyboard interrupt service address
2 0			<reserved>
3 0			<reserved>
4 0			keyboard buffer location
5 0			mapped memory
6 0			mapped memory
7 0			mapped memory
8 0			mapped memory
9 0			mapped memory
10 0			mapped memory
11 0			mapped memory
12 0			mapped memory
13 0			mapped memory
14 0			mapped memory
15 0		0	stack area - program return address
16 0			stack area
17 0			stack area
18 0			stack area
19 0			stack area
20 0			stack area
21 0			stack area
22 0			stack area
23 0			stack area
24 0			stack area
25 0			stack area
26 0			stack area
27 0			stack area
28 0			stack area
29 0			stack area
30 0			stack area
31 0			stack area
32 0			stack area
33 0			stack area
34 0			stack area
35 0			stack area
36 0			stack area
37 0			stack area
38 0			stack area
39 0			stack area
40 0			stack area
41 0			stack area
42 0			stack area
43 0			stack area
44 0			stack area
45 0			stack area
46 0			stack area
47 0			stack area
48 0			stack area
49 0			stack area
50 0			stack area
51 0			stack area
52 0			stack area
53 0			stack area
54 0			stack area
55 0			stack area
56 0			stack area
57 0			stack area
58 0			stack area
59 0			stack area
60 0			stack area
61 0			stack area
62 0			stack area
63 0			stack area
64 0			stack area
65 0			stack area
66 0			stack area
67 0			stack area
68 0			stack area
69 0			stack area
70 0			stack area
71 0			stack area
72 0			stack area
73 0			stack area
74 0			stack area
75 0			stack area
76 0			stack area
77 0			stack area
78 0			stack area
79 0			stack area
80 0			stack area
81 0			stack area
82 0			stack area
83 0			stack area
84 0			stack area
85 0			stack area
86 0	RTS		print string routine - R0=string address
87 0	RTS		print number routine - R0=number
88 0	RTS		get string routine - R0=string buffer, R1=buffer size
89 0	RTS		get number routine - returns number in R0
90 0			<reserved>
91 0			<reserved>
92 0			<reserved>
93 0			<reserved>
94 0			<reserved>
95 0			<reserved>
96 0			<reserved>
97 0			<reserved>
98 0			<reserved>
99 0			<reserved>
100 0	move	#'F',5	;move character "F" to memory address 5
101 0	move	#'R',6	;move character "R" to memory address 6
102 0	move	#'A',7	;move character "A" to memory address 7
103 0	move	#'N',8	;move character "N" to memory address 8
104 0	move	#'C',9	;move character "C" to memory address 9
105 0	move	#'I',10	;move character "I" to memory address 10
106 0	move	#'S',11	;move character "S" to memory address 11
107 0	rts		
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

I did handyboard ASM once.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
Gil_Hamilton
Buzzkill Gil
Posts: 4294
Joined: Wed Jan 12, 2005 7:14 pm

Post by Gil_Hamilton »

I've done MIPS. My college ASM class used a REAL instruction set.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

The reserved stuff is as interesting as a bag of bricks.

You don't paste cstdio everytime you paste a c++ program, do you ?
Haneda wrote:What I am told is an example of how strings actually work in high-level languages:

Code: Select all

100 0	move	#'F',5	;move character "F" to memory address 5
101 0	move	#'R',6	;move character "R" to memory address 6
102 0	move	#'A',7	;move character "A" to memory address 7
103 0	move	#'N',8	;move character "N" to memory address 8
104 0	move	#'C',9	;move character "C" to memory address 9
105 0	move	#'I',10	;move character "I" to memory address 10
106 0	move	#'S',11	;move character "S" to memory address 11
107 0	rts
Constant strings are stored in memory and referred to by the pointer to their first character.
Typically if you compile for size a loop will be used, but if you compile for speed the loop will be unrolled and that's the result.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
odditude
Official tech support dood
Posts: 2118
Joined: Wed Jan 25, 2006 7:57 am

Post by odditude »

Gil_Hamilton wrote:I've done MIPS. My college ASM class used a REAL instruction set.
indeed. think i lost that code 7 years ago, though.
where's my .text and .data?
Why yes, my shift key *IS* broken.
Gil_Hamilton
Buzzkill Gil
Posts: 4294
Joined: Wed Jan 12, 2005 7:14 pm

Post by Gil_Hamilton »

odditude wrote:
Gil_Hamilton wrote:I've done MIPS. My college ASM class used a REAL instruction set.
indeed. think i lost that code 7 years ago, though.
where's my .text and .data?
Yeah, same here.

I still have the book, though!
odditude
Official tech support dood
Posts: 2118
Joined: Wed Jan 25, 2006 7:57 am

Post by odditude »

Gil_Hamilton wrote:
odditude wrote:
Gil_Hamilton wrote:I've done MIPS. My college ASM class used a REAL instruction set.
indeed. think i lost that code 7 years ago, though.
where's my .text and .data?
Yeah, same here.
I still have the book, though!
i do too, but it's still packed away after two moves. no clue where.
Why yes, my shift key *IS* broken.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

odditude wrote:where's my .text and .data?
between the .bss, foo !
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
PHoNyMiKe
Retrosexual
Posts: 1011
Joined: Wed Jul 28, 2004 2:09 am
Location: Rapture

Post by PHoNyMiKe »

nice work, here's some atmel microcontroller 8bit assembly, how to read a snes controller.

Code: Select all

.dseg
snes_upper:				;holds currently read buttons, 1 = pressed, 0 = not
	.db		0x00
snes_lower:
	.db		0x00
snes_upper2:			;holds previous buttons
	.db		0x00
snes_lower2:
	.db		0x00
snes_upper_single:		;holds bits for buttons previously off, but then pressed
	.db		0x00		;aka single click, clear bits manually after reading them
snes_lower_single:
	.db		0x00
.cseg


		;===SNES controller
.equ	PORT_SNES = PORTB
.equ	PIN_SNES = PINB
.equ	DDR_SNES = DDRB
.equ	SNES_CLOCK = PB0
.equ	SNES_LATCH = PB1
.equ	SNES_DATA = PB2

		;===SNES controller pinout
		;1		5 volt		White
		;2		Clock		Yellow
		;3		Latch		Orange
		;4		Data		Red
		;7		Ground		Brown

		;===ram snes_upper bit defines
.equ	SNES_B = 0
.equ	SNES_Y = 1
.equ	SNES_SELECT = 2
.equ	SNES_START = 3
.equ	SNES_UP = 4
.equ	SNES_DOWN = 5
.equ	SNES_LEFT = 6
.equ	SNES_RIGHT = 7
		;===ram snes_lower bit defines
.equ	SNES_A = 0
.equ	SNES_X = 1
.equ	SNES_L = 2
.equ	SNES_R = 3

read_snes:
	push	r16
	push	r17
	push	r18

	lds		r16, snes_upper				;copy new input into old input
	sts		snes_upper2, r16
	lds		r16, snes_lower
	sts		snes_lower2, r16

	;begin reading controller
	sbi		PORT_SNES, SNES_CLOCK		;clock starts/rests at high
	sbi		PORT_SNES, SNES_LATCH		;tell controller to latch all inputs
	cbi		PORT_SNES, SNES_LATCH		;drop latch, inputs can be read

	clr		r18					;register will store button presses
	ldi		r16, 0x08			;read 8 bits
	clc
	read_snes_loop:				;reads B, Y, Select, Start, Up, Down, Left, Right
	in		r17, PIN_SNES
	sbrs	r17, SNES_DATA
	sec							;sets carry high if button is pressed (logic 0)
	ror		r18					;shifts carry into r18
	cbi		PORT_SNES, SNES_CLOCK
	sbi		PORT_SNES, SNES_CLOCK
	dec		r16
	brne	read_snes_loop
	sts		snes_upper, r18

	clr		r18					;register will store button presses
	ldi		r16, 0x08			;read 8 bits
	read_snes_loop2:			;reads A, X, L, R
	in		r17, PIN_SNES
	sbrs	r17, SNES_DATA
	sec							;sets carry high if button is pressed (logic 0)
	ror		r18					;shifts carry into r18
	cbi		PORT_SNES, SNES_CLOCK
	sbi		PORT_SNES, SNES_CLOCK
	dec		r16
	brne	read_snes_loop2
	sts		snes_lower, r18
	;done reading controller


	lds		r16, snes_upper		;check for button down upper byte
	lds		r17, snes_upper2
	mov		r18, r16
	eor		r18, r17		;make sure the button is either being pressed or released
	and		r16, r18		;make sure button is only being pressed
	sts		snes_upper_single, r16

	lds		r16, snes_lower		;check for button down lower byte
	lds		r17, snes_lower2
	mov		r18, r16
	eor		r18, r17		;make sure the button is either being pressed or released
	and		r16, r18		;make sure button is only being pressed
	sts		snes_lower_single, r16

	pop		r18
	pop		r17
	pop		r16
	ret

init_snes:
	sbi		DDR_SNES, SNES_LATCH
	sbi		DDR_SNES, SNES_CLOCK
	cbi		DDR_SNES, SNES_DATA

	ret
[url=http://www.alexchiu.com/affiliates/clickthru.cgi?id=phonymike]ultimate immortality[/url]
[url=http://www.sloganizer.net/en/][img]http://www.sloganizer.net/en/image,zsnes,white,purple.png[/img][/url]
I.S.T.
Zealot
Posts: 1325
Joined: Tue Nov 27, 2007 7:03 am

Post by I.S.T. »

Given he's been banned, you posted that for naught.
Rashidi
Trooper
Posts: 515
Joined: Fri Aug 18, 2006 2:45 pm

Post by Rashidi »

.text
why it called ".text" ?
i can understand why the other was .data or .stack or .<what-ever>seg(-ment), but .text ?

enlight me.
blargg
Regular
Posts: 327
Joined: Thu Jun 30, 2005 1:54 pm
Location: USA
Contact:

Post by blargg »

text n.
1.a. The original words of something written or printed, as opposed to a paraphrase, translation, revision, or condensation. b. The words of a speech appearing in print.
2. The body of a printed work as distinct from headings and illustrative matter on a page or from front and back matter in a book.
[...]
PHoNyMiKe
Retrosexual
Posts: 1011
Joined: Wed Jul 28, 2004 2:09 am
Location: Rapture

Post by PHoNyMiKe »

I love asm, it's so rad.
[url=http://www.alexchiu.com/affiliates/clickthru.cgi?id=phonymike]ultimate immortality[/url]
[url=http://www.sloganizer.net/en/][img]http://www.sloganizer.net/en/image,zsnes,white,purple.png[/img][/url]
h4tred

Post by h4tred »

Code: Select all

 MOV EBX,EAX
  	XOR ECX,ECX
  	XOR EAX,EAX
  	MOV EDI,OFFSET szName
  	MOV EDX,0DEADC0DEH

	loop1:

  	MOVSX EAX,BYTE PTR DS:[ECX+EDI]
	ADD EAX,EDX
        IMUL EAX,EAX,0666H
        ADD EDX,EAX
        SUB EAX,0777H
        INC ECX
        CMP ECX,EBX
        JNZ loop1
        MOV EBX,EAX
        XOR EAX,EAX
have phun
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

Code: Select all

%macro KeyTabInc 1-* ; take this, dood
  push edx
%rep %0
  xor edx,edx
  mov eax,[%1]
  div dword[%1+4]
  mov [%1],edx
  %rotate 1
  or [%1],eax
  cmp eax,1
  je %%exit
  %rotate -1
  sub eax,[%1]
  adc dword[%1],0
  jnz %%exit
  %rotate 1
%endrep
%%exit
  pop edx
%endmacro
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
h4tred

Post by h4tred »

nice work grin.

Completely outshows the rip that I posted (was from a super simple keygenme.)
Post Reply