Programming in assembly

Reading Time: 4 minutes

Assembly is the programming language closest to a CPU. ‘Closest’ here means that the keywords in the language translate 1:1 into instructions done by the CPU. With other (higher lever) languages, there’s either an interpreter or compiler which unpacks the language and generates usually quite significantly higher number of instructions when the code is to be executed on a real CPU.

With assembly, you tell exactly the processor what you want done – at register level. Whereas higher level languages achieve a lot of operations by abstracting the ingredients into a function (subroutine) name, assembly is about the elementals of computation: manipulating bits, and registers (composed of 8, 16, 32, or 64 bits at a time) — essentially, one of the smallest storage units of digital computers.

The things we often learn as programmers is a bit more “sophisticated”, and rightly so! It’s good to work on a level we’re comfortable with.

All languages, deep down “enough”, will be compiled into assembly. For example, Java is compiled to Java bytecode, which is then run in a virtual machine called JVM. The JVM however has to eventually execute plain assembly. Same with all other languages.

A (high level) programming language can be either:

  • interpreted, or
  • compiled

The question between those two choices has mainly to do with: at which point does the conversion to machine language happen; is it “early on” (compiled languages), or during the execution (interpreted languages). Python is interpreted, while C language is a compiled language. C produces traditional executable files; whereas Python source code is run by passing the file to the Python interpreter.

Assembly is a good language to really get an understanding of what the computing hardware actually does. All modern computers are described with the van Neumann architectural model:

A computer simply can load binary values to registers; do comparison and the usual suspects like addition, subtraction, multiplication, and division; store the value back to RAM (memory); and do stuff like jump around in various parts of the code (the ‘IF… THEN’ equivalent).

At first the basic level of profiency in assembly is attained by learning the opcodes: what commands are available. In reality, even assembly commands are internally stored and executed as a sequence of microcode within the processor.

Think of registers as just 8-, 16- 32 or 64 bit variables. They are done in real gates, physical constructs in the CPU. So they “always exist”, fixed. Their value can be altered: you can load a number, or you can load a number (content) from a memory location. There are commands to

  • zero a register (make it 0)
  • add two registers (arithmetically sum)
  • subtract a register’s value from another register
  • multiply
  • divide a number in a register by another register
  • compare the values of registers (and take action: a jump = branch)

I did a lot of Intel x86 assembly programming as teen.

Is assembly really that hard?

Why does assembly have a hard-to-grasp reputation? It’s probably because of the very terse and “weird” vocabulary. Also compared to other languages, there’s so much of “nonsensical” stuff in assembly: why the heck do you “move the value 64778 to register this-or-that”.. It doesn’t seem to make any sense at all!

When you’ve learned to program in assembly, it all makes sense. But I have to admit that looking at some of the code now, in the year 2019 – that’s some 25 years later – I don’t recollect all the details anymore.

Let’s look at a image uncompression program. It’s a complete program, showing a RIX image on-screen. RIX is a format which is now almost extinct. It used to be quite popular in the wild, although very simple format. Because of being simple the .RIX was also a perfect training target for making a program that can interpret it.

.286c
KOODI SEGMENT PARA 'CODE'
ASSUME CS:KOODI, DS:TIETO
INCLUDE LIBS\JS1.INC

Set_DTA proc near
mov ah,1ah
lea dx,new_dta
int 21h
ret
Set_DTA endp

AllocMem proc near
mov ah,48h
mov bx,4096
int 21h
jnc yli1
disp_str nomem
end_prog 255
yli1:
mov alseg,ax
ret AllocMem
endp

DeAllocMem proc near
push es
mov ax,alseg
mov es,ax
mov ah,49h
int 21h
pop es
ret DeAllocMem
endp

;; Find first file, matching the search mask string defined
;; in memory area pointed to by "maski"
FindFirst proc near
mov ah,4eh
xor cx,cx
lea dx,maski
int 21h
ret
FindFirst endp

;; After we have called once the FindFirst proc,
;; continue giving next results using the same search mask string
FindNext proc near
mov ah,4fh
int 21h
ret FindNext
endp

LoadRIX proc near
lea dx,dta_name
call open_file
mov kahva,ax
mov bx,ax
call begin_file
mov bx,kahva
mov cx,64778
xor dx,dx
push ds
mov ax,alseg
mov ds,ax
call read_file
pop ds
mov bx,kahva
call close_file
ret
LoadRIX endp

SwitchPic proc near
push ds es
mov ax,0
mov w1,ax
mov w2,ax
mov ax,alseg
mov es,ax
spl:
mov cx,3
plp:
in al,60h
loop plp
mov si,w1
add si,w2
cmp si,030ah
jb eikay
cmp si,0fd09h
ja eikay
mov al,byte ptr [es:si]
push ds ax
mov ax,0a000h
mov ds,ax
pop ax
mov byte ptr [si-030ah],al
pop ds
eikay:
inc word ptr [w1]
cmp w1,0ffffh
jne yli3
pop es ds
ret
yli3:
mov ax,w1
add w2,ax
jmp spl
SwitchPic endp

ClearBuf proc near
push es
mov ax,alseg
mov es,ax
xor si,si
xor ax,ax
cbl1:
mov word ptr [es:si],ax
add si,2
cmp si,0fd10h
jb cbl1
pop es
ret ClearBuf
endp

PROSED PROC FAR
mov ax,tieto
mov ds,ax
call Set_DTA
call FindFirst
jnc yli2
disp_str norix
end_prog 255
yli2:
call AllocMem
mov ax,13h
int 10h
newpic:
call LoadRIX
push es
mov ax,alseg
mov es,ax
mov dx,000ah
xor bx,bx
mov cx,256
set_block
call SwitchPic
call FindNext
jc ulos
get_key 0
cmp al,27
je immed
jmp newpic
ulos:
get_key 0
call ClearBuf
call SwitchPic
immed:
mov ax,3
int 10h
call DeAllocMem
mov ax,0c06h
mov dh,0ffh
int 21h
end_prog 0

PROSED ENDP KOODI ENDS TIETO
SEGMENT PARA 'DATA'
w1 dw 0
w2 dw 0
maski db '*.rix',0
nomem db 'Not enough free memory (64K) to run program!$'
norix db 'No .RIX files found in current directory!$'
alseg dw 0 kahva
dw 0 new_dta db 30 dup(0) dta_name
db 13 dup(0) TIETO
ENDS
END prosed

Smartphones inside out – part 2: mobile networks

Reading Time: 14 minutesI wrote a post about mobile phones a “bit” earlier. This is the follow-up, with mobile networks. They are, inevitably, something that the whole mobile culture relies on. Yet I think mobile networks have received quite little interest from general public.

5G is a hot topic right now in 2018. The generations of mobile networks mean that there is an incremental continuation in improving (often) the speed, coverage and generally the capacilities of the mobile network. 5G has been a long time in the waiting. There are stepping stones to it, from the current 4G, so the change is not a “turn of a switch”.

Let’s get back to basics.

I was fascinated with the mobile masts early on. img_8169I’m not “into them” so much nowadays, but remember scaling one mast and checking it out myself as a teen.

During studies I was part of a local radio amateur club (OH2TI) for a couple of years in Otaniemi, 2005 and 2006 (or thereabouts). This was much better way to get familiarized with masts, radio technology, electronics, and people.

Mobile networks are the almost invisible part of our mobile culture. Without them, there wouldn’t be “mobility” at all – we’d just be carrying phones that could not seamlessly connect to other phones, servers, and landline phones. Proliferation of the network’s components has led to better coverage and a faster network. The support structure in densely populated cities is different from rural areas. However the ingenuity of a mobile network is that it unifies the layer so that users feel as if the network is magically omnipresent.

Looking back now in 2017 to the roots of telecommunication, it’s easy to almost forget what a long journey had to be taken to get the quality of mobile networks available today.

As the story of Nokia Mobile Phones has somewhat waned from public limelight, the Nokia (network company) goes on strong.  tn_siilasmaaIn fact, in autumn 2018 Risto Siilasmaa is publishing a book about the current Nokia!

Network speeds in mobile world have gone through the ceiling, almost. A 50 mbit/s mobile download speed would have been pure fantasy just a decade ago.

Nowadays it’s quite evident that mobile networks are used for 2 prime purposes: transmitting speech and transmitting data. Speech used to be the sole type of payload going between a mobile phone and the network. Nowadays the roles have almost swapped: people use data, by using their smartphone apps, possibly more than they’re using talk.

SMS or short text messages was a curious “freak invention”. People could send a maximum of 160 characters from a phone to another. SMS did not technically “consume” bandwidth between the hops in mobile network, since the message payload data was transmitted in a control channel. SMS became a killer app; it is even still used a lot, even though there are a lot of “competing” applications that utilize data, and can provide a richer means to transmit icons, animations and photos along with text.

How does a mobile talk to the mobile network?

A mobile network faces the phones using radio. Radio itself is an older invention. The modern digital mobile networks always have to, nevertheless, work according to and respecting radio principles. That’s part of “why”: we need standards bodies to regulate radio traffic, otherwise an ensuing chaos would not have enabled the development of such widespread and homogenous quality adaptation of mobility.

Session between phone and mast

So, img_8170each mobile phone has a “discussion” (ongoing session) with a mast. For example our speech becomes a stream of bits: first the mobile phone digitizes the voice of its user, then sends this on wire. The “wire” happens to be radio, not a cable — but do remember, often the gap between masts is indeed a wire ūüôā

In 1990s the antenna design race was a big one. There was also a settling period between whether the antenna should be visible (a whip) or embedded cleanly into the phone. The latter won.

All things are layered, so that for example a software developer does not have to know all the gross details of radio networks. Even the mobile phone manufacturer doesn’t these days have to know all the details: manufacturers can subcontract the manufacture and design of electronic chips used in the phones.

Mobile network speed

The speed comes in three different aspects: latency, bandwidth and the network coverage. A fourth aspect is jitter (the variability of quality during time).

In reality there is also a 5th aspect that is not actually about the mobile network; but rather the load of an application server. Just as in desktop computers, some sites might have inadequate capacity, considering the number of users, and thus the user experience is not the best possible.

The latency is the initial setup time of a data connection, and it is also the minimal time that it takes to get any amount of data from transmitting phone to a receiving phone (or server). Latency affects the quality of real-time communications like speech, video and gaming. Less is better. Nowadays latency as low as 5-10 milliseconds is achievable in mobile networks. When first data capabilities appeared in mobile networks, the setup might take several 1000s of milliseconds.

Why the mobile network speed is tricky?

In mobile networks you are always out there, in the wild: things are a little bit more unpredictable than those office LANs. The amount of cell towers, their interconnect network quality, temporary issues that have to do with weather, natural and built obstructions between phone and the mast; network outages, and many other things affect your perception of the quality of a mobile network.

When you buy a mobile lease, that is, sign up with a mobile operator, you’re buying a snapshot of their offering: how things stand today. That is, quality and prices can change in the future. If the operator doesn’t expand capacity as fast as it gains new customers, then on average the throughput will decline.

Sometimes bottlenecks in capacity are not directly up to the operator – they might face legislative, technical or even political reasons for not being able to change the network structure.

Evolution of mobile tech

15 years back the phone was most about 2 things: built-in features and a fixed software. The pace of development was fast, but it was in a different way: every release of a new phone model was anticipated by questions regarding what Truly New Features were packed into the make and model.

Phones were also exploring the form factor quite radically in the 1990s:whether the phone was a single-piece or had a bevel (joint) and a separate screen; how many real buttons the phone had. The design of the buttons (ordinary, 4 directions, a “navi” roller etc.).

At some point completely new things, like the digital camera, surprised the markets. Camera was an interesting thing, especially now judged afterwards. It became an essential ingredient of the smart phone.

World first in camera phones? Samsung with the model

Then further down the line, innovations became kind of more anticipated: you’d get still leaps of new exciting development, but it was¬†incremental, not disruptive. iPhones have basically stopped evolution since iPhone 4: even the staff at Apple Stores roll their eyes when being asked about “What’s the next iPhone going to bring?”. Answer: “A little bit of this and that – nothing magic.”

Apps, apps, more apps!

Nowadays there are millions of apps on app stores. An average consumer keeps about 17 apps per phone.

Thus the competition for “attention” on a user’s phone is fierce, even though technically the limit to hold more apps is starting to vanish. People just don’t want too much clutter on that very personal real estate. Typically the choice to start an app requires some swiping left/right on the phone. The more you have apps, the longer it takes to get to the correct one. It’s a usability issue.

Today’s 1 mbit/s, 5, or even 40-50 mbit/s burst download speeds are insanely fast compared to the almost magical 9600 bit/s that initially was allocated to a phone by its “host”, the mobile tower. Today’s speeds are thus

Let’s put the Moore’s law to test. Moore can predict to the future of technological evolution. It’s an empirical formula, based on the initial observations of how gates (components) in integrated chips got smaller. This ‘minituriazation’ is the key phenomena that enables high-tech products.

Evolution of the Mobile Network

How slow or fast were things with a early “smartphone”?

When Nokia’s “Communicator“, a flagship product, hit the road in 1996, and soon became a legend, the mobile data network was quite different back then. A phone’s data connection practically rode at most on max 2 combined GSM data channels. Each data channel packed 9,6 kbit/s so the Communicator could run at a then-whopping 19200 bits/s.

Let’s take as example one digital image, around 128 kilobytes, and that would take 54 seconds to download. Impractical. Nowadays in 2018 the download time is in reality somewhere between 1 and 2 seconds. Not bad at all! Let’s say it is 25 x faster now.

By the way, taking that image download example, with 5G coming in 2018-2020 these kinds of small UX improvements are expected to get “just right” – that’s my bet. There’s also something that tech itself can improve. The way those images are being used can affect the various parts of the timing, and thus our psychological feeling of how fluently the particular service works.

Let us hop back to 1996 and the Communicator:

in practical sense, for example most emails were only pure ASCII text – something around 1-2 kilobytes per mail; thus 20 new mails could be downloaded in 18 seconds. Or a single email in about 3-5 seconds, with all the protocol set-up etc that took place when the client-server connection was established. It wasn’t exactly as fluid as you’d want, but then again: it was quite a breakthrough, a shift in the paradigm: you did not have to be sitting at your desk. You were suddenly truly “mobile”, capable of doing most parts of work anywhere. Well, anywhere within the reach of mobile networks.

The 19200 bps speed we mentioned is a tiny fraction of the speeds of nowaday’s 4G networks. It’s less than 1 percent of 4G’s speed. Imagine that!

Still the 1990s Nokia Communicator was a phenomenal success story at that time. It was iconic, and it also brought significant power to the owner.

Handover and the magic of data

A mobile phone, in order to stay useful and true to its breed, keeps a connection to its “parent” – the mobile mast. The phone basically only peeks into the world through its data connection. This is the very core phenomena of “the world is at your fingertips” -illusion: yes – and no. Only the next “hop” (a PC with a network connection) is at your phone’s fingertips. If that server machine – the “receiving end” of your mobile data connection – is not up, then you’ll take a dive into the abyss: “no connectivity”. However during times the technology has been evolving and almost 100% of the time you will have a data connection. Almost magic!

The scenario is not that much different from your laptop and the WiFi access point (“hotspot”). With mobile phones, the distances are greater, and usually there’s also a rather constant movement – thus a “handover” is expected. In handover the mobile phone¬†leaves¬†one network and enters another network. In many cases, this is completely automatic and the user doesn’t know a handover took place.

(By the way; depending on the design, if in the “laptop scenario” we mentioned before, you leave the range of your hotspot, you’ll¬†lose¬†connectivity. Any attempt to access Internet will show an error message. It is possible to design campus networks so that there are Wi-fi¬†repeaters within the area, densely enough, so you can carry around and do work with your laptop, and it jumps from a hotspot to another as needed).

Looking at the feature list of a mobile (cellphone) sometimes makes us dizzy. The lists, as fair and accurate they might be, don’t tell the whole story. Usually just acronyms are used: WCDMA, 3G, GSM, WiFi, UMTS, EDGE, and so on.¬†I think there’s a vast population of users to whom these words mean absolutely nothing, or, sometimes even worse, they vaguely bring into mind something which practically leads to expensive or inefficient use of the phone.

On manufacturer side, 3G and 4G networks have become de facto. The availability and quality is still an ongoing quest, worldwide, and there are huge differences in average density and speeds per country.

Without proper knowledge one can use the phone in very unoptimal way, pay too much fees, and get frustrated because of network outages. Knowledge never hurts.

GSM – the foundations of mobility

The three letters “GSM” used to be eponymous for the whole handset industry. GSM originates from a standards group (originally¬†Groupe Sp√©cial Mobile ), a very small one, which drafted the technology that united a back-then quite heterogeneous set of mobile network technologies. One crucial example: handover.

When you use a mobile phone in car or while walking, you cross boundaries between different base stations. A base station can be “heard” only for about a few kilometers – beyond that the signal fades off. A handover happens when two base stations agree that your mobile phone call will be continued even after crossing the boundary.

GSM is the main standard that enables worldwide mobile communications. GSM was immensely importance at its time, and still guarantees interoperability of the phones at voice and text message level.

A couple of rule of thumbs:

– know your phone’s settings: what your phone’s hardware (features) allow, what is available to be modified as settings, and where you can find them in the menus

– different network technologies have varying maximum speeds, physical ranges, and security. Use the one that is suitable for your purposes.

Analog and digital networks

I’ll leave largely the software part aside, and concentrate on those (physical) network features. The original cell phones used different kind of technology: analog networks.

These were very much the same as what radio amateurs had been using for decades: one’s voice is transferred into a different frequency, mixed with a carrier wave, and transmitted. On the other end, a reverse process happens and the listener receives the original voice.

While these were simple, analog networks had problems with security, conflicts (overhearing other people’s talk), and the inability to serve as a backbone for modern services. Thus GSM standard was born. The word ‘GSM’ came to mean also the cell phone itself. GSM is an interoperability standard which defines digital communications between phones, using mobile base stations (‘masts’).

The neat idea of digitization is that once you have that basic ‘pipe’ or connection between two phones, you can do a lot of things over it. Text messages, Web, using email, and a lot of other¬†services became possible. A flagship product of this kind of “mobile office” was probably Nokia’s Communicator 9000.

Antennas

But as simple as we’d like to keep the digitization, the truth is a bit more complex. The basic reason behind this is that still, no matter how digital, the communications happens in the electromagnetic spectrum – in the air, using electrons. Electrons are guided by antennas. They fly through air (kind of – unfortunately; electrons would be ideally precise in void, but we would have trouble without air to breathe ūüėČ and are received by another antenna – the receiver.

This movement of electrons (electricity) obeys the laws of physics, which don’t give a heck about what’s going on the “upper plane”. All communications that we use in today’s world (2016) happens using some band (slice) of a frequency.

Where do WiFi, Bluetooth, 3G, 4G come in?

The thing to note first is that phone communications can be of three distinct types:

  1. autonomously between two phones
  2. phone using local wireless area network (WiFi)
  3. phone using data network (3G, 4G)
The first type of communication includes Bluetooth and infrared (IR). It’s just about “swapping” data, when the phones are relatively close to each other. The significance of phone-to-phone communications has declined, as it is plain easier to use ritually a “TCP/IP” communications method, ie the Internet. Another thing that diminishes the practicality of phone-to-phone is that large business platforms like Facebook, Google tools and a host of others all benefit most if all data goes through a central point (their servers).
However, where autonomous communication is important, will be between the mobile phone and local information systems like ticket vending, or a car’s entertainment system.
It doesn’t cost anything, because there is no operator in between. The phones usually negotiate a security code (PIN) by asking the users to agree on one. People use this kind of comms to swap addresses, copy files, and so on. The speeds vary, Bluetooth being faster than infrared. BT is also more secure and robust to interference. Also NFC (near field communications) is a type of autonomous communications, where the receiver is often a POS device at a shop.
Where does one need WiFi?
That’s actually a very neat feature, that can save you money and allow doing software installations, updates, and operating system installations easier. WiFi means that your phone essentially looks like a computer to a WiFi hotspot device: the device allows you to become part of the network, essentially Internet.
The most obvious place to have WiFi is at your home or work. Also public places like libraries, cafes, and metropolitan areas in general can have a network coverage.
WiFi cannot charge (make you pay) directly, but you may not receive access rights to WiFi network in some places, before you pay. For example, some hotels, airports, or other similar places require you to buy a coupon which gives you a username and password, or might otherwise authorize you to use the network.
The ‘native’ data communication happens using 3G or 4G networks. These are mechanisms by which your phone can relay information between any other computer (server) that would basically be reachable via Internet. When you load a Web page, read your email, or do anything that comes from the Internet, your phone will most likely use these networks (but the preference, order of networks is configurable).
Note that 3G and 4G data has a real price. There are several billing plans (‘data plans‘): by amount of data being transferred, a fixed data plan (monthly costs), temporary data plan (usually bought for one day) – actually too many to exhaustively list all of them. What matters is that you know your plan type.
The most dangerous situation is when you are not sure about what kind of plan you have, and still continue using the data features: this puts you into jeopardy of getting a very hefty bill the next time your operator sends you one. So: be sure! Use the Internet (on a PC), or call your operator to get timely information of the plans.
What’s the future of mobile computing and phones?
– “no one knows”
– ..but still we might have good guesses
Understanding the market drivers
– companies
– temporary financial drivers in components and manufacture profitability
– large handset manufacturers are also interested more and more about network quality
– B2B sales drive security needs: corporations are obliged by law to have security standards, and part of this is the mobile security
Many smartphones become severely “impeded” without a good quality mobile network. The phone/network symbiosis has become evident. A way to test that out is to turn off the mobile data in your own phone. Then the apps will (only) function mostly when you’re in the reach of a free Wifi. Some apps however are useful even in this off-line usage mode.
One can still home in to Wifi networks here and there, but without mobile data coverage, the phone
Theoretically ordinary mobile operator’s services could be hosted on a server, and let the phones roam in Wi-fi -like networks. Thus this would open up the traditionally very “closed-domain” role of a mobile operator. But there are strong economic incentives that direct the path of technological advances in things like mobile networks: carrier’s one of the most valuable and hard-to-imitate asset is exactly this “core mobile network”.
There have been these kind of tests; for example in the city of Oulu, Finland. Oulu is one of the birth places of mobile telecommunications, and has a pioneering attitude to develop even radically new kinds of mobile paradigms. City of Oulu provided a “Finland first” in communal, free wireless network.
Why haven’t the data speeds grown faster?
Reality vs. what we think
– network coverage
– true average attainable speeds
What’s keeping us back from enjoying high-quality, non-interrupted TV on a mobile phone? The answers are variable, and there’s a lot behind the development. Some of the things we’re experiencing are because of heavy standardization processes. Mobile communications is a field that needs rules for the players, in order to make it possible for a large group of users enjoy the experience. If there was no standardization of radio frequency use, we would essentially be electronically jamming each other and no one would be able to get data through.
Networking in general had problems in the 1970s and 1980s due to inconsistent technologies, which meant that even within a single building there might have been half a dozen different network technologies. (By the way, Cisco – the network company – was built out of the vision that these networks should be interconnected and working together!)
This same kind of problem was troublesome for WiFi, too – at first incompatibilities seemed to be a major obstacle to success. When technology is in its infancy, consumers are often very skeptical – and, when the benefits start to show up, so do new customers line up.

Final words

Mobile networks arose from a simple idea: carry voice and data, through a backbone, and “surface” the coverage with a radio connection. Thus you would create a network that would allow you, the user, roam around freely and still be connected. The first analog mobile networks were already tried in the 1970s. GSM standard was a major miletsone in the unification and standardization of digital communication in the mobile networks. Data usage has overtaken voice during the 2000s. Cravings for ever greater speeds, coverage and flexibility drives the evolution of mobile networks, now entering the 5G era in around 2018-2020 throughout the world.

DONT’T CLIMB THE MASTS…

By the way! img_8167¬†I really don’t recommend scaling the mobile masts. Rather, pass this article on, and/or drop a question here, right in the comments. This way we’ll get much more information on this modern day marvel of mobile networks.

Best use for a planar 890..960 MHz antenna?

Reading Time: < 1 minuteAll your ideas belong to here. That’s a Huber’n Suhner planar antenna, wall-mountable, for
the 890..960 MHz band. Shoot!

Any ideas would be welcome.

This particular item was one of the most interesting pieces of hardware found in the corridors of Aalto computer science building. There’s a table in the corner of the local library, where departments and individuals can place abandoned stuff. I occasionally drop in and check what’s there on offer.

This time I found three large(ish) printers, but they somehow spelled “trouble”. I thought I’d just end up with 25kg of trash if the printer doesn’t work.. so the antenna seemed wiser choice.

I wonder what this has been used for in the past?