convert a boolean expression in 16f877a assembly language  - Page 1
Forum section: electronics / general

author: heithem
author_rank: Newbie
timestamp: December 05, 2020, 03:42:16 pm
content: ***16f877a assembly***Hi guys i want to program thiswe consider the following logical equationS1 = A'BC'DS1 = (not(A))and(B)and(not(C))and(D)variables A B C D are respectively connected to lines RA0 RA1 RA2 RA3 Of portA and function S1 is connected to line RB0 Of portBwrite the assembly program which allows this function to be carried out

author: Ian.M
author_rank: Super Contributor
timestamp: December 05, 2020, 04:41:52 pm
content: Homework problem?One approach to implementing the four input AND would be to set a flag bit, then test each condition in turn with BTFSS or BTFSC instructions, each skipping an instruction to clear the flag. If it remains set, all the conditions were true. Finally, copy the flag bit to the output port bit, and loop back to do it all again.Another and far more efficient approach would be read all the inputs simultaneously from port A, mask out the unwanted bits and directly compare the remaining bits read with the desired result, then set the output bit accordingly.

author: bson
author_rank: Unknown
timestamp: December 05, 2020, 08:37:15 pm
content: Read RA into a register so that bits RA3 RA2 RA1 RA0 = D C B ACheck if the value is 1010 = 0xaIf so, set RB0, otherwise clear it.

author: MIS42N
author_rank: Frequent Contributor
timestamp: December 08, 2020, 11:20:00 am
content: Something like:MOVF PORTA,WANDLW 0x0F ; leave Ra0 to RA3XORLW 0x0A ; test conditionBTFSS STATUS,Z ; skip if condition satisfiedBCF PORTB,0BTFSC STATUS,Z ; skip if condition not satisfiedBSF PORTB,0

author: Ian.M
author_rank: Super Contributor
timestamp: December 08, 2020, 11:42:15 am
content: @ MIS42N,Its general forum etiquette *NOT* to post detailed solutions to academic exercises (and suspected ones), as not letting the student work it out for themselves actively harms their education.If/When they get stuck, we are of course willing to help, either with general suggestions or by correcting problems with their posted attempt at a solution.

author: tggzzz
author_rank: Super Contributor
timestamp: December 08, 2020, 03:08:15 pm
content: Quote from: Ian.M on December 08, 2020, 11:42:15 amAn old tradition is to reply with something superficially plausible but fundamentally wrong. The studentoughtto be able to spot that; if not then they and their supervisor have learned something

author: ozcar
author_rank: Frequent Contributor
timestamp: December 08, 2020, 07:31:14 pm
content: Quote from: tggzzz on December 08, 2020, 03:08:15 pmIt has been a long time, about twenty years, since I programmed a PIC, but I think that the proposed solution is superficially plausible, but incomplete. Is that good enough to uphold the tradition?

author: MIS42N
author_rank: Frequent Contributor
timestamp: December 08, 2020, 10:26:28 pm
content: Quote from: Ian.M on December 08, 2020, 11:42:15 amYes, sorry about that. I've only been posting for a week or so, mainly about the lars GPSDO. Didn't know about the etiquette. I've been programming assembler on and off for over 50 years and until retirement my job had a big component of sorting out problems. Add that I am currently working on an assembler program for a PIC16F1455 and maybe you can understand why I thoughtlessly jumped in.I learn a lot by seeing a solution and teasing it apart to see why it works. The solution I gave may be optimal (couldn't be done more efficiently - I haven't checked) and it is very unlikely a student would come up with it. An alert teacher might see that and ask for an explanation.OK. More restraint next time. Thanks for the heads up.

author: ozcar
author_rank: Frequent Contributor
timestamp: December 09, 2020, 02:29:22 am
content: Quote from: ozcar on December 08, 2020, 07:31:14 pmSo, I was sitting outside in the sunshine (I too am retired), and I got to thinking about this (I too used to be a problem solver). I realised that not only is the proposed solution (arguably) incomplete, it does happen to be fundamentally wrong - so no harm done!

author: MIS42N
author_rank: Frequent Contributor
timestamp: December 09, 2020, 09:20:35 am
content: Quote from: ozcar on December 09, 2020, 02:29:22 amI did say 'something like', but I thought it was close. I sent you a PM, ask for clarification away from public eyes. It seems I have more to learn.

author: ozcar
author_rank: Frequent Contributor
timestamp: December 09, 2020, 11:08:08 am
content: Quote from: MIS42N on December 09, 2020, 09:20:35 amI could try saying that I only posted that to cast doubt and make Heithem think about it, but no, I was just plain wrong - really I should just know better than to try to work things out while dozing.

author: Ian.M
author_rank: Super Contributor
timestamp: December 09, 2020, 01:20:25 pm
content: @MIS42N,<posted in error>

author: MIS42N
author_rank: Frequent Contributor
timestamp: December 09, 2020, 09:50:03 pm
content: Quote from: Ian.M on December 09, 2020, 01:20:25 pmThanks for the offer, but I'm on top of this. Read Reply #7 - been doing this for 50 years. ozcar was stirring.I am using MPASM with MPLAB X v5.35 and I won't be going to XC8 - my view is it for C programmers who insert snippets of assembler. It has nowhere near the capabilities of MPASM. Maybe one day the 8-bit processor will be no more, until then I believe MPASM has a place.My current project is 3500 lines of assembler and growing, for a PIC16F1455. It handles 6 interrupt sources including a 40kHz one to dither the PWM to emulate a 24 bit DAC. It incorporates a 32 bit signed arithmetic core (originally by Peter Hemsley) modified to operate on a stack. This is used for several operations, the main one a least squares linear calculation on data points. I have a 3.3V source of serial data sending to a 5V processor, and the RX port is CMOS levels and should have a level shift. Instead I wrote a software UART receiving on a TTL level pin. This has the possible benefit of freeing the RX port for user input (I was thinking of using the USB port but that was a learning curve I'm not prepared to climb just yet). Also a comparator is used to gate TMR1 to time an arriving pulse to the nearest 25nS. There's a primitive thread system needed to decode the serial data while servicing the interrupts and running the calculations.I wondered if the software UART would interest others. It uses IOC (interrupt on change) and TMR0 to process a 9600 baud stream, the interrupt servicing is quite short (under 30 instructions after sorting out the source of the interrupt) and it uses 2 bytes of data. The threading system is used to process each character once assembled - lots of instructions - but that is dependent on the application (in this case decoding the NEMA data from a GPS module).

author: Ian.M
author_rank: Super Contributor
timestamp: December 09, 2020, 10:26:34 pm
content: @MIS42NVery sorry. I confused you with the O.P.Its a great pity Microchip didn't package MPASMX as a separate legacy language tool a while back. It was obvious the writing was on the wall for it once C18 was depreciated as no other language could link with MPASM assembler modules, and debug symbol support for it under MPLAB X was IIRC pretty flaky.A nice software UART receive routine would certainly be of interest. Is it very Enhanced Midrange core specific or could it reasonably be ported to PIC18?@HeithemShow us what you've written so far, and tell us what doesn't work, and we'd be delighted to help you. If you are having difficulty getting started writing any code, we can also help with that e.g. by directing you to the tools you need and possibly directing you to template code for the PIC you are using.N.B. Microchip recently depreciated MPASM assembler and replaced it with XC8's ASPIC assembler. They use very different directives and slightly different instruction syntax so compatibility is poor. If you need to use MPASM, you need MPLAB X v5.35 or earlier.

author: brucehoult
author_rank: Super Contributor
timestamp: December 10, 2020, 01:19:38 am
content: Quote from: MIS42N on December 09, 2020, 09:50:03 pmHas there ever been a true 8 bit microcontroller?I mean:- 8 bit ALU and datapath- 8 bit opcodes- 8 bit data memory addresses- 8 bit program memory addresses and PCThe 8051 is the closest I have personal knowledge of (and that is still available) but even it always had more than 256 bytes of program space as far as I know. Or perhaps that was just the first resource to be extended.PICs have 8 bit ALU and datapath, but *none* of the other attributes are 8 bit.It seems to me there will always be a need for processing data that is mostly limited to 8 bits. It's a convenient size for "good enough" analog conversion of many things, and of course convenient for text, and not too wasteful for boolean data.Designs with bank switching, addressing that wraps around inside a "page", all calculation funneled though a single accumulator, poor stack support, and so on -- those ones can die in a fire. It can't be too soon.I've never actually compared PIC code directly against AVR, MSP430, or Thumb but it certainly looks as if it's pretty awful in size, as well as being a pain to write.In a discrete MCU chip there is no point in having these days in having the smallest simplest core possible as you have minimum physical size dictated by the pins on the package, and minimum power use dictated by the drivers for the pins. There is absolutely no reason except software compatibility not to put a small 16 or 32 bit core in there.For a core sitting on the edge of a larger chip and controlling something the size matters more, but you still often want to be able to address everything on the larger chip, which means 32 bit at least. I know SiFive was surprised how many requests they got for Cortex M0 class cores with 64 bit addressing -- and with 16 registers instead of 32. RV32I, RV64I and RV32E were expected configurations, but it's turned out customers actually want RV64E also (and it's trivial to do).I don't know anything about MPASM but I suppose I can guess. IBM has always had very powerful assemblers for their mainframes, and back in the 80s Apple had an M68000 assembler that was largely modeled off IBM's. It was powerful enough to conveniently build Object Pascal or C++ classes and virtual function tables & dispatch using assembler macros and interoperate with those languages at an advanced level (not just a C API).

author: MIS42N
author_rank: Frequent Contributor
timestamp: December 10, 2020, 02:14:58 am
content: Quote from: Ian.M on December 09, 2020, 10:26:34 pmYou have me at a disadvantage with the PIC18. I've not looked at it (until a few minutes ago). It looks like the code would work using one of TMR2/4/6, they look like enhanced TMR0. Memory access all looks different but the logic still applies.I did a cut and paste of the bits from the program, with some explanation. It was written by me for me so doesn't follow any conventions. I've attached it, would be interested in feedback.

author: MIS42N
author_rank: Frequent Contributor
timestamp: December 10, 2020, 03:39:23 am
content: Quote from: brucehoult on December 10, 2020, 01:19:38 amI don't think anyone would make such a thing. The closest I can think of is the Electronics Australia educ-8 - but it had a serial ALU so arguably 1 bit?I cut my assembler teeth on an IBM 360/30. It ticks a few boxes - 8 bit instruction, 8 bit data path, 8 bit ALU backed up by a massive amount of microcode to make it look like a 32 bit machine. Some experience with ICL 1900 assembler (not a byte machine - 24 bit words with 4 6-bit characters). One of the instructions on the ICL was DVS - divide single length. DVD divided a 24 bit number into a 48 bit number held in two registers [e.g. DVD 5,7 - divide number in 7+8 by number in 5. I've forgotten the actual details) which would yield the integer result in one register and the remainder in another. DVS did the same except the mnemonic was DVS 5,7 when the number to be divided was actually in register 8. I remember this because the instruction was DeViouS (cue groan).The venerable 6502 (as used in Commodore 64, Apple II, Atari) has 8 bit instruction, 8 bit data, 8 bit ALU but to be useful has a 16 bit address bus.QuoteTend to agree. I think they still exist because of the body of existing software and the ability to run on minimal power. The enhanced architecture overcomes some of the limitations. I struggled with the PIC16F628, tried to do too much with too little. Much happier with the PIC16F1455. I think the PIC architecture is designed with lowest power in mind. If a peripheral isn't used, its power is shut off. The low power version can do processing on 15uA from a 1.8V supply. That's impressive.I'm attracted by the price. I can do what I want with a processor costing less than $1. And I like the challenge. Perhaps it's masochism. I did write some programs using a soft implementation of a single instruction computer (SUBLEQ - subtract A from B and branch to C if result is less than or equal to zero). Challenging. As long as the code can modify itself, and some data can start off with initial values, it can do most things.

author: brucehoult
author_rank: Super Contributor
timestamp: December 10, 2020, 06:08:35 am
content: Quote from: MIS42N on December 10, 2020, 03:39:23 amATTiny85 is under $0.95 qty 1 in 20 MHz 2.7-5.5 V -40 to +85 with MLF-20 package, a little over $1 in DIP. That's 8 kB flash (4096 instructions), 512 B SRAM, 512 B EEPROM, 8 pins, 6 I/Os (4 ADC).Your PIC16F1455 is $1.36 in the cheapest version at Mouser. It is a slightly bigger part with 14 kB flash, 1024 B SRAM, 11 I/O. No non-volatile RAM? 48 MHz, but given the ISA it's probably a little slower than 20 MHz AVR.The PIC16F628 is smaller than either (224 B RAM!, 2.5k flash, no ADC), but over $2.I'm well familiar with the 6502, still remember a dozen or two hex opcodes 40 years after learning them. I even bought a couple of 14 MHz 65C02 chips ($8 each) and 6522 VIAs recently to play with. And some parallel EEPROM, SRAM, PSRAM, and Flash chips.

author: MIS42N
author_rank: Frequent Contributor
timestamp: December 10, 2020, 09:21:42 am
content: Quote from: brucehoult on December 10, 2020, 06:08:35 amYou are of course right, the PIC16F1455 is over a $1. But why let the truth ....The whole of program memory is non-volatile RAM, i.e. flash memory. The enhanced architecture allows programs to write to program memory and read it as data. I came across a USB boot loader for it, haven't looked at it in detail yet, part of the USB learning curve. Memory 1F80h-1FFFh is designated high endurance flash 100K writes. My program uses it, fiddly but works.I think the PIC v Atmel discussion is tribal, they have strengths and weaknesses. Atmel is very register oriented, memory access is mostly limited to load and store. If most processing can be kept in registers, goes very fast. But something like a shift of a memory location is load/shift/store taking 3 instruction and 5 clock cycles. The PIC does it in 1 instruction and 4 clock cycles.2 years ago I wrote a top octave generator for the ATmega328P with a 16MHz clock. Simultaneous producing 13 square waves from 4184 Hz to 8368 Hz. It flogged the registers and used memory as a big circular buffer. I don't know if a PIC could do it.But the PIC (enhanced) has a much better interrupt architecture saving and restoring the state of several registers so the interrupt routine can assume a clean slate. The 628 didn't do that so saving and restoring the W and status could be a big overhead.Horses for courses.

author: ozcar
author_rank: Frequent Contributor
timestamp: December 10, 2020, 08:03:52 pm
content: Quote from: MIS42N on December 10, 2020, 09:21:42 amI still have a couple of real MK50240 TOG chips. One is plugged into a tone generator board replete with twelve SAJ110 dividers, and also 13 transistors that I used to tweak the MK50240 output level to match that of the SAJ110s. The transistors are marked "IBM291" - they were scavenged from a core memory driver board from an IBM 370/155, which was perhaps the last IBM mainframe to use magnetic core memory. Ah, the good old daze.Getting a Arduino Pro-Mini to do that is an impressive feat. However, these days something like a STM32 Blue or Black Pill board can be had for practically the same price, and with one of those it might be possible to avoid the problem you mentioned elsewhere of not being able to update all thirteen of the outputs simultaneously.

author: brucehoult
author_rank: Super Contributor
timestamp: December 10, 2020, 09:51:30 pm
content: Quote from: MIS42N on December 10, 2020, 09:21:42 amPIC and 8051 are I think optimized around very simple applications where you read some input value and tweak some output value with very little computation in between. They dedicate a large chunk of the instruction encoding to directly fiddling with bits in registers.AVR, MSP430, ARM are maybe not quite so concise for fiddling register bits, but are much better for large applications, and processing-intensive applications. And applications written in C rather than assembly language. There are C compilers for PIC but the ISA doesn't make it easy for them!

author: MIS42N
author_rank: Frequent Contributor
timestamp: December 10, 2020, 10:43:41 pm
content: Quote from: ozcar on December 10, 2020, 08:03:52 pmIt was my first (and so far, only) program for the ATmega328P. I bought some pro minis to monitor a solar system I started to build, and the TOG was used as a learning experience. I find I need a target to get the learning juices running. The solar system is still half built - mañana.I have so far skirted around ARM processors. I haven't had the right target. The TOG project was 'good enough' - in my opinion nobody would notice the 60nS offset of one square wave to another. So I won't be revisiting it. I thought it had sunk without trace but now I know otherwise. Sunk with a ripple.I regret not keeping some old tech. At one time I had a 1950's teletype that had done only a few hours work, complete with tape reader and punch and all documentation. Also a working key to tape data entry machine (it had a core memory) and a washing machine sized disk unit (8MByte I think). We moved over 30 years ago and they were flogged off. I still have an evaluation kit for the Intel 4040, and several PCs from the early 80s - Atari, Commodore 64, Osborne - but none of them see light of day. I'd like to see someone set up a computer museum in Newcastle, they'd get a few donations from me. But I don't think it will happen.