The Novels

Economics 101, a Novel (Rough Draft) -- My first sustained attempt at a novel, two-thirds finished in rough draft, and heading a little too far south.
What would you do if you and your study partner, with whom you had been seriously discussing marriage, suddenly found yourselves all alone together on a desert island? Study economics?
Sociology 500, a Romance (Second Draft) -- The first book in the Economics 101 Trilogy.(On hold.)
Karel and Dan, former American football teammates and now graduate students, meet fellow graduate students Kristie and Bobbie, and the four form a steady study group.

Featured Post

Sociology 500, a Romance, ch 1 pt 1 -- Introducing Bobbie

TOC Well, let's meet Roberta Whitmer. Bobbie entered the anthropology department office and looked around. Near the receptionis...

Monday, January 13, 2020

Backup: 33209: School -- BASIC vs. Pascal vs. Assembler

[JMRbefore20210330.]Backup of https://joelrees-novels.blogspot.com/2020/01/33209-school-basic-vs-pascal-vs-assembler.html.

Chapter 3.4: School -- Shoe Sizes, Flowcharting, and Pseudo-code

Chapter 3.5: School -- BASIC vs. Pascal vs. Assembler

Professor Crane set aside the slides he had been using to explain the simple address book program that would be our next assignment and picked up another set. 

"I'm sure y'all all want to talk about Pascal some more."

Scattered chuckles and groans greeted the pronouncement.

"Well, we're going to talk a little more about Pascal today so we can talk about something Professor Bright and I are studying, called structured programming." He paused.

"There are dialects of BASIC in existence which don't use line numbers. And you can write programs in those without using a GOTO."

"Cool."

"Wow!"

"Help?"

"Why not just move to Pascal instead of ruining BASIC?"

I was not one who said any of the above. I think my response was, "So, these versions of BASIC look kind of like Pascal?"

"Kind of."

"How does that even work?" asked Becky.

"Well, let's talk about structured programming." He waited for the chatter to stop. "In BASIC, you can write code that looks like a plate of logical spaghetti with strands of program flow going all over the place and getting all tangled up in a mess. In Pascal, it's a lot harder to write spaghetti code. But even in BASIC, you can usually untangle that spaghetti into more understandable sequences of just a few kinds of fundamental elements." He put a slide on the overhead projector.


"This is one fundamental element, the block. And on the right is a sequence of blocks."

Lisa asked, "Is a block like a line of BASIC?"

"Or of Pascal. A line or a group or lines can be a block. The idea is that together they perform a single function. And you can treat them like a single block. Ideally, you start in at the top of the block and end at the bottom. Pascal also allows you to explicitly declare a block using the BEGIN and END statements."

He waited for a moment so we could copy the slide into our notes. Then he traded the slide for another.

"This is the decision, conditional, or branch."

"It's the BASIC IF statement," asserted Daren.

"Very good. It gets constructed with the IF statement, and often requires GOTOs in the usual dialects of BASIC. The GOTO will jump around the false and true blocks for he the condition George, can you write us an example on the board?"

George went to the board and wrote:

1000 REM TWO-WAY DECISION
1010 IF V1 = V2 THEN GOTO 1100
1020 REM FALSE BRANCH
1030 A1=B1
1040 REM ETC.
1090 GOTO 1200
1100 REM TRUE BRANCH
1110 B1=A1
1120 REM ETC.
1190 REM END OF BLOCK
1200 REM SOMETHING ELSE CONTINUES FROM HERE.

Instead of copying what he wrote, I doodled on the back of a program listing:

* 2-way decision/branch (conditional)
* IF V1 = V2, 32-bit integer V1 and V2
	MOVE.L vV1,D0
 	SUB.L  vV2,D0
 	BNE IFNEV1V2  ; Inverted test.
* LET A1 = B1
MOVE.L vA1,vB1 ; true branch * etc. BRA IFDONEV1V2 IFNEV1V2 ; false branch * LET B1 = A1 MOVE.L vB1,vA1 * etc. * end of block IFDONEV1V2 * continues from here

Several of the students were still busily taking notes when George sat down. 

Professor Crane asked for questions, but the students who were lost just gave him blank looks. So he continued, "In Pascal, that would look like this." He took out a clean slide and put it on the projector, and wrote:

{ Two-way decision }
  IF V1 = V2 THEN 
  BEGIN
    { True branch! }
    B1 := A1
    { Etc. }
  END
  ELSE
  BEGIN
    { False branch. }
    A1 := B1
    { Etc. }
  END
  { Something else continues from here. }

"Why do I have the false and true branches reversed here from the BASIC code?"

Lisa suggested, "Because the test will make a jump, and you jump to the true branch, but, uhmm," she thought, then continued slowly, "... just let the program flow into the false branch?"

Becky exclaimed, "Oh, I get it." She hesitated. "I think."

"Good. Anyone not getting it?"

No response.

Professor Crane stood up and walked around the room, checking students' notes. He stopped at my desk. "May I?"

I looked up at him questioningly. 

"Take a look at your assembler notes. Is this 68000 assembler?"

"Yeah. I didn't want to think too hard about integer size." I held the page up, and he accepted it.

"Mind putting this up on the board to show the class?"

"Sure. I mean, no. No problem." I stood and went to the board, repeating the doodled code from memory.

Professor Crane pointed to the first line. "Is this a comment?"

"Yeah. Comment lines start with asterisk in Motorola assemblers."

"Interesting." He pointed to the third line. "Move dot 'L'?"

"Long is 32 bits. Dot 'W' would be word, or sixteen bits. Dot 'B' would be byte, or eight bits."

"So that's a 32-bit integer move. 8086 only does 16-bit. You put 'v' in front of the variable names?"

"Otherwise, A1 would, uhm, the name of variable A1 would conflict with the name of address register A1."

"I see. Okay, Intel syntax has the target first, as move to target from source. I guess Motorola syntax is more English-like, move from source to target?"

"Yeah. Data register zero is the target of the first move. I'm using subtract for the compare in the next line because," I hesitated here, "the compare is just a subtract that doesn't store it's result, and I'm pretty sure it's the same order as subtract, but I didn't want to think about it. Not that it matters here, since we're only testing equality."

Professor Crane snorted a chuckle and shook his head. "How many registers does the 68000 have?"

"Eight data registers, D0 through D7, and eight address registers, A0 through A7. Ehh, plus a second A7. A7 is the stack pointer for subroutine calls, and there's a user A7 and a system A7." 

"That's a lot of registers. Does it have an accumulator?"

"Any data register can be an accumulator."

He raised his eyebrows and turned back to the listing. "Okay, 'BNE' is?"

"Branch if not equal. Not equal to zero. Inverted condition so I could write the true branch first. Branch to," I spelled it out: "'I-F-N-E-V-1-V-2'. That label is supposed to mean 'If not equal V1 and V2'."

"You made the label up."

"Yep." 

"And labels are?"

"Machine language has addresses instead of line numbers, but when you edit code, the code moves around, so the addresses change. So the assembler lets you give a location a name, and then the assembler figures out the address for you."

"So it's like a line number, but different."

"Right."

"Okay. Is that a memory-to-memory move in the next instruction, direct, without using a register?"

"Yeah. Target is the second argument."

"Again, opposite Intel assembler order. And that is not a reference to women's underwear in the next instruction?"

There was a wave of snickers and titters.

"Branch always," I answered apologetically. "I think I would have just used 'BR' or written it out 'BRANCH', but I'm not the one who made up the mnemonics."

Professor Crane nodded. "I hope none of the women in the class are offended. Macros would allow that instruction to be renamed, by the way."

Lisa cleared her throat. "How nice of them." She and Pat exchanged looks.

Pat shrugged. "I wish we didn't have to put up with immature male engineers. It sure isn't an ideal world."

Dirk let out a horse laugh. "Yer im-mah-choor, Joe."

Lisa gave him a glare. 

"Okay, okay, maybe I'm a little immature, too, sometimes." Dirk was still laughing, but not out loud. "How about you, Joe?"

I rolled my eyes and shrugged. "Probably not as mature as I ought to be all the time."

"So we'll both try to grow up?" He grinned at me.

"Yeah."

Lisa sniffed and turned away. Since she was between Dirk and me, she was now looking at me. She tried to keep a straight face, then broke out laughing when I shrugged apologetically again. She looked back to Pat and said, "We're holding these guys to their promise, right?" 

Pat nodded, her face showing mixed feelings.

"We'll work on it." I reached over and bumped fists with Dirk.

"Is one- or two-way branching all there is in structured programming?" Mike asked.

Professor took the cue. "There is also a case construct, but it can be built from these two. We can look at it later. I want us to look at loops now." 

"Wait," Becky hold up her hand. "Don't you need more branches to surround the false and true blocks?"

I turned back to the whiteboard and pointed to the code between the branch not equal and the branch always. "Since the test is inverted, the false case branches over the true case block." I pointed to the branch always. "The true case ends by branching over the false case block to the label 'IFDONEV1V2'. But, since that label is just the address of the instruction after the false case block, the program flow naturally joins back together there without a branch."

Becky still looked a little puzzled, but she nodded.

I sat down.

Professor Crane had swapped slides while we were discussing the program flow.

"Here we are. These are the two loops generally discussed in structured programing."

Mike asked, "Is there a mid-test loop?"

Professor Crane pulled his mouth to the side.

I looked up and added, "As in a general loop with a block before and after the test?"

Mike glanced at me and nodded. 

Professor Crane still hesitated.

Mike stood. "Can I draw out what I mean?"

"Go for it." Professor Crane held out a marker.

Mike took it and drew this:

Professor Crane nodded thoughtfully. "That's a general loop. There are languages that have such loops built-in, but most structured programming practice suggests against it, from what I understand. Pascal doesn't have one."

Mike turned back to the board and wrote out example BASIC code for the loop to the side:

2000 REM LOOP STARTS HERE
2010 REM PRE-TEST BLOCK
2020 A=B+C
2030 REM ETC.
2100 IF A=E THEN GOTO 2200
2110 REM POST-TEST BLOCK
2120 A=-B-C
2130 REM ETC.
2190 GOTO 2000
2200 REM PROGRAM CONTINUES FROM HERE

"And the pre-test and post-test loops would just have one of those blocks empty," he commented as he put the marker back in the tray.

Alex complained, "What does that code even do?"

Mike frowned and cocked his head. "It's just example code."

"Just enough," I looked up from my assembly language doodling, "to see the connection between the BASIC and the flowchart." 

I had this scribbled down at this point:

LOOPSTART
* Pre-test block
* A=B+C
MOVE.L vB,D0
ADD.L vC,D0
MOVE.L D0,vA
* Etc.
* IF A=E THEN GOTO exit
CMP.L vE,D0 ; D0 - vE
BEQ LOOPEND
* Post-test block
* A=-B-C
CLR.L D0
SUB.L vB,D0
SUB.L vC,D0
MOVE.L D0,vA
* Etc.
LOOPEND 

Mike frowned and turned back to the whiteboard. He picked up the eraser, but Professor Crane said, "We have lots of whiteboard space."

So he put the eraser down and started writing more code in an open spot:

2000 REM GET NEXT SHOE SIZE
2010 INPUT "SHOE SIZE ('Q' FOR QUIT):"; R$
2020 IF LEFT$(1) = "Q" THEN GOTO 2200
2030 R = VAL( R$)
2040 REM ...
2200 GOTO 2000

"That is a more real example. After we talk a bit more about the basics, I can show you how that's done with a conditional inside a post-test loop." Professor Crane swapped slides on the projector again and proceeded to use the remainder of the period to lead the class through comparing the structured flow elements in Pascal, BASIC, and, borrowing more of my doodling, assembly language. Our assignment was a simple address book program using BASIC DATA statements to build separate arrays of names, addresses, and phone numbers.

(Maybe I don't need to say this again, but I will. The Joe of this novel is way ahead of the Joe of the real world. I had only begun to pick up the rudiments of 6800 assembler during the first semester back from my mission. Translating BASIC or Pascal to 68000 assembler during a lecture would have been me at least a year later, probably two. 

And I wasn't that good with banter. Still not, really.)

TOC

[Current backup at https://joel-rees-economics.blogspot.com/2020/01/bk-33209-school-basic-vs-pascal-vs-assembler.html.
Developed February and March 2021, notes at https://joel-rees-economics.blogspot.com/2020/01/notes-33209-school-basic-vs-pascal-vs.html.
Extracted and expanded from https://joel-rees-economics.blogspot.com/2020/01/bk01-33209-school.html.
Earlier backup at https://joel-rees-economics.blogspot.com/2020/01/bk-33209-school.html.]

No comments:

Post a Comment

Keep it on topic, and be patient with the moderator. I have other things to do, too, you know.