判断质数汇编程序编写

以下面的代码为例,说明如何用汇编语言判断一个数是否为素数,从而进行汇编语言的学习。 点击展开/折叠代码 .MODEL SMALL .STACK 100H .DATA msg_input DB ‘Please input a number: $' msg_prime DB 0DH, 0AH, 'It is a prime number.$’ msg_not DB 0DH, 0AH, ‘It is not a prime number.$’ num DW 0 .CODE START: MOV AX, @DATA MOV DS, AX ; 1. 显示输入提示 LEA DX, msg_input MOV AH, 09H INT 21H ; 2. 读取数字 (读取一个十进制数) CALL READ_NUM MOV num, AX ; 3. 判断素数逻辑 ; 如果 n < 2,不是素数 CMP AX, 2 JL NOT_PRIME JE IS_PRIME ; 2 是素数 ; 循环判断: 从 2 到 n-1 MOV CX, 2 ; CX 是除数 CHECK_LOOP: MOV AX, num XOR DX, DX ; 清除高位 DIV CX ; AX / CX, 余数在 DX ...

January 12, 2026 · 3 min · farmer3-c

convex review

Lecture 1: Unconstrained Optimization for Differentiable Functions 几个无限制可微问题的优化: 对于一般的函数求极值问题: 一个变量的函数可以求导,极值一般在导函数的零点。 如:求极小值$f(x)=x^2-1$ 多个变量的函数可以求偏导,极值一般在偏导函数的零点。 如:求极值$f(x,y)=x^3-y^3+3x^2+3y^2-9x$ 对得到的四个驻点进行的二阶导数检验(Hessian 判别法) $$|H|=f_{xx}f_{yy}-(f_{xy})^2$$令$a=f_{xx}$ 若 ∣H∣>0且 a>0 → 局部极小值(Ext. small) 若 ∣H∣>0 且 a<0 → 局部极大值(Ext. large) 若 ∣H∣<0 → 鞍点(uncertain,即非极值点) 若 ∣H∣=0 → 无法判断(需要更高阶检验) 梯度下降求极大/极小值: 对一个复杂的函数求极值是一件困难的事,梯度下降可以使它变得简单。 $$ \begin{aligned} f(x) &\approx f(x_0)+(x-x_0)f'(x_0) \\ f(x) &= f(x_0)-\Delta xf'(x_0) \end{aligned} $$$\implies x=x_0-\Delta x$ 设置一个学习率a,或者叫步长, $\Delta x=a f'(x_0)$ 然后就是一个迭代的过程: $x_{k+1}=x_k-\Delta x_k$ $f'(x_k)=0$时到达极值点。 牛顿法-I解决等式限制的优化: 思想也是迭代。 如:$e^x-2x^2+3x-4=0$ 迭代$(x_n,f(x_n))$ ${y=f'(x_n)(x-x_n)+f(x_n)} \xrightarrow {\text{y → 0}}{x=x_n-\frac{f(x_n)}{f'(x_n)}}$ ...

December 11, 2025 · 7 min · farmer3-c

matlab tutorial

1.Basic Arithmetic 直接在命令行输入算式,结果存入ans变量中 如: >> 1+2 ans = 3 2.Variables 设置变量的值,存入系统中,之后可以调用 >> x=7 x = 7 >> x+3 ans = 10 >> 3.Change Format format [数据类型] 可以改变运算的结果数据类型 >> 1/3 ans = 0.3333 >> format short >> 1/3 ans = 0.3333 >> format long >> 1/3 ans = 0.333333333333333 >> 4.Remove Variables clear [变量名] 用于清楚变量 >> x x = 7 >> clear x >> x 函数或变量 'x' 无法识别。 >> 5.Clear Specific Variables 高级一点的Remove Variables之类的用法 >> x3=3 x3 = 3 >> >> clear x* >> x3=3 x3 = 3 >> who 您的变量为: ans x3 >> whos Name Size Bytes Class Attributes ans 1x1 8 double x3 1x1 8 double >> 6. Pre-Defined Constants 系统中预定义的变量 ...

November 22, 2025 · 5 min · farmer3-c

静态随机存储器

静态随机存储器实验 实验目的 (1) 掌握静态随机存储器 RAM 工作特性及数据的读写方法。 (2) 基于信号时序图,了解读写静态随机存储器的原理。 实验设备 PC 机一台,TDX-CMX 实验系统一套。 实验原理 实验所用的静态存储器由一片 6116(2K×8bit)构成(位于 MEM 单元),如图 2-1-1 所示。 6116 有三个控制线:CS(片选线)、OE(读线)、WE(写线),其功能如表 2-1-1 所示,当片选有效(CS=0)时,OE=0 时进行读操作,WE=0 时进行写操作,本实验将 CS 常接地。 实验原理图如图 2-1-2 所示,存储器数据线接至 CPU 内总线,内总线上接有 8 个 LED 灯显示 D7…D0 的内容。地址线接至地址总线,地址总线上接有 8 个 LED 灯显示 A7…A0 的内容,地址由地址锁存器(74LS273,内嵌于 ABI 单元)给出。数据开关(位于 CON 单元的 SD17..SD10)经一个三态门(74LS245)连至 CPU 内总线,分时给出地址和数据。地址寄存器为 8 位,接入存储器的地址 A7…A0,高三位地址 A10…A8 接地,所以其实际容量为 256 字节。 实验箱中所有单元的时序都连接至时序与操作台单元,CLR 都连接至 CON 单元的 CLR 按钮。实验时 T3 由时序单元给出,其余信号由 CON 单元的对应二进制开关模拟给出,其中 RD、 WR 低有效,SW_B 低有效,LDAR 高有效。 ...

November 5, 2025 · 2 min · farmer3-c

线段树

线段树是一种特殊的数据结构,它可以在 $O(\log n)$ 的时间复杂度内实现单点修改、区间修改、区间查询(区间求和,求区间最大值,求区间最小值)等操作。 最简单的线段树的构建: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 #include <iostream> #include <vector> using namespace std; // 定义线段树的节点 struct Node { int l, r; // 左右子节点的下标 int sum; // 区间和 }; vector<Node> tree; // 存储线段树的数组 // 构建线段树 void build(int l, int r, int index, vector<int>& nums) { tree[index].l = l; tree[index].r = r; if (l == r) { // 叶子节点 tree[index].sum = nums[l]; return; } int mid = (l + r) / 2; build(l, mid, index * 2, nums); // 构建左子树 build(mid + 1, r, index * 2 + 1, nums); // 构建右子树 tree[index].sum = tree[index * 2].sum + tree[index * 2 + 1].sum; // 计算区间和 } // 区间查询 int query(int l, int r, int index) { if (tree[index].l >= l && tree[index].r <= r) { // 当前区间完全包含在查询区间内 return tree[index].sum; } int mid = (tree[index].l + tree[index].r) / 2; int sum = 0; if (l <= mid) { // 查询区间和左子树有交集 sum += query(l, r, index * 2); } if (r > mid) { // 查询区间和右子树有交集 sum += query(l, r, index * 2 + 1); } return sum; } // 单点修改 void update(int pos, int val, int index) { if (tree[index].l == tree[index].r) { // 叶子节点 tree[index].sum = val; return; } int mid = (tree[index].l + tree[index].r) / 2; if (pos <= mid) { // 修改的位置在左子树 update(pos, val, index * 2); } else { // 修改的位置在右子树 update(pos, val, index * 2 + 1); } tree[index].sum = tree[index * 2].sum + tree[index * 2 + 1].sum; // 更新区间和 } int main() { vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 原始数组 int n = nums.size(); // 数组长度 tree.resize(n * 4); // 线段树的大小为 4n build(0, n - 1, 1, nums); // 构建线段树 cout << query(0, 4, 1) << endl; // 查询区间 [0, 4] 的和 update(2, 10, 1); // 将第 2 个元素修改为 10 cout << query(0, 4, 1) << endl; // 查询区间 [0, 4] 的和 return 0; } 构建解释: 递归地将数组分为左右两个子数组,直到子数组长度为 1,此时该子数组的区间和就是该子数组的元素值。 ...

October 20, 2025 · 3 min · farmer3-c

什么是无线网卡

什么是无线网卡? 无线网卡,又称无线网络卡或 Wi-Fi 卡,是一种硬件组件,它使计算机或智能手机等设备能够连接到无线网络。它允许设备通过无线电频率信号与其他设备或接入点进行通信和数据交换,无需物理电缆。 无线网卡的优点是什么? 无线网卡最显著的优势在于其自由移动性。用户无需通过线缆被限制在特定位置,即可连接网络和访问互联网。 无线网络可以同时容纳多台设备,因此具有高度的可扩展性。这在许多设备需要连接到同一网络的环境中尤为重要,例如拥挤的公共场所或企业环境。 无线网卡在连接和实现物联网设备之间的通信方面起着关键作用。它们促进传感器、设备和中央服务器之间的数据交换,推动物联网生态系统的增长 无线网卡是如何工作的? 无线网卡通过使用射频信号来传输和接收数据。它们连接到网络的接入点,为数据流创建一条虚拟路径。这些网卡采用先进的加密技术来确保数据安全,使无线通信既高效又安全。 What is a Wireless Card? And How Does it Work? Wireless security

October 16, 2025 · 1 min · farmer3-c

Introduction to Linear Programing

What is Linear Programming? Linear programming (LP), also known as linear optimization, is a powerful mathematical method used to find the best possible outcome in a given situation. It’s used to achieve results like maximum profit or minimum cost, where the objective and the constraints are expressed as linear relationships. In essence, LP provides a systematic way to solve optimization problems. Standard Form To solve a linear programming problem, we first need to express it in standard form. This is the most common and intuitive way to structure an LP problem and consists of three key components: ...

September 14, 2025 · 5 min · farmer3-c

machine learning

机器学习 definition 机器学习(ML)是人工智能的一个研究领域,关注于开发和研究能够从已有数据中学习并推广到未见数据,从而无需明确指令就能执行任务的统计算法。在机器学习的一个子学科中,深度学习领域的进展使得神经网络——一种统计算法——在性能上超越了许多之前的机器学习方法。 统计学和数学优化(数学规划)方法是机器学习的基础。数据挖掘是一个相关的研究领域,专注于通过无监督学习进行探索性数据分析(EDA)。 从理论角度来看,大概近似正确学习为描述机器学习提供了一个框架。 history 机器学习一词由 IBM 员工、计算机游戏和人工智能领域的先驱亚瑟·塞缪尔于 1959 年提出。在这个时期,也使用了同义词“自教学计算机”。 最早的机器学习程序出现在 20 世纪 50 年代,当时亚瑟·萨缪尔发明了一个计算机程序,用于计算跳棋双方获胜的概率,但机器学习的历史可以追溯到人类数十年来研究认知过程的愿望和努力。1949 年,加拿大心理学家唐纳德·赫布出版了《行为的组织》一书,书中介绍了一种由神经细胞之间特定相互作用形成的理论神经结构。赫布的神经元相互作用模型为人工智能和机器学习算法在节点(计算机用于通信的人工神经元)下如何工作奠定了基础。其他研究人类认知系统的研究者也促进了现代机器学习技术的发展,包括逻辑学家沃尔特·皮茨和沃伦·麦克洛克,他们提出了早期的神经网络数学模型,以形成模拟人类思维过程的算法。 Modern day Machine Learning algorithms are broken into 3 algorithms types: Supervised Learning Algorithms, Unsupervised Learning Algorithms, and Reinforcement Learning Algorithms. Current Supervised Learning Algorithms have objectives of classification and regression. Current Unsupervised Learning Algorithms have objectives of clustering, dimensionality reduction, and association rule. Current Reinforcement Learning Algorithms focus on decisions that must be made with respect to some previous, unkown time and are broken down to either be studies of model based methods, and model free methods. theory 学习者的一个核心目标是泛化其经验。在此文中,泛化是指学习机器在经历学习数据集后,能够对新未见过的示例/任务进行准确执行的能力。训练示例来自某个通常未知的概率分布(被认为是发生空间的代表),而学习者必须构建一个关于这个空间的通用模型,使其能够在新情况下产生足够准确的预测。 ...

September 14, 2025 · 2 min · farmer3-c

datalab

CS:APP Data Lab The purpose of this lab is to become more familiar with bit-level representations of integers andfloating point numbers. You’ll do this by solving a series of programming “puzzles.” Many of these puzzles are quite artificial, but you’ll find yourself thinking much more about bits in working your way through them.———— Harry Bovik bitXor bitXor - x^y using only ~ and & Example: bitXor(4, 5) = 1 Legal ops: ~ & Max ops: 14 Rating: 1 1 2 3 4 5 int bitXor(int x, int y) { // x^y=(x&~y)|(~x&y)=~(~x&~y)&~(x&y) return ~(~(x & ~y) & ~(~x & y)); } tmin tmin - return minimum two’s complement integer Legal ops: ! ~ & ^ | + « » Max ops: 4 Rating: 1 1 2 3 4 int tmin(void) { return 1 << 31; } isTmax isTmax - returns 1 if x is the maximum, two’s complement number, and 0 otherwise Legal ops: ! ~ & ^ | + Max ops: 10 Rating: 1 1 2 3 4 int isTmax(int x) { return !(~(x + 1) ^ x) & !!(x + 1); } allOddBits allOddBits - return 1 if all odd-numbered bits in word set to 1 where bits are numbered from 0 (least significant) to 31 (most significant) Examples: allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1 Legal ops: ! ~ & ^ | + « » Max ops: 12 Rating: 2 1 2 3 4 5 6 int allOddBits(int x) { int m = (0xAA << 8) | 0xAA; m = (m << 16) | m; return !((x & m) ^ m); } negate negate - return -x Example: negate(1) = -1. Legal ops: ! ~ & ^ | + « » Max ops: 5 Rating: 2 1 2 3 4 int negate(int x) { return ~x + 1; } isAsciiDigit isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters ‘0’ to ‘9’) Example: isAsciiDigit(0x35) = 1. isAsciiDigit(0x3a) = 0. isAsciiDigit(0x05) = 0. Legal ops: ! ~ & ^ | + « » Max ops: 15 Rating: 3 1 2 3 4 5 6 int isAsciiDigit(int x) { int a = x + (~0x30 + 1); int b = 0x39 + (~x + 1); return !((a >> 31) | (b >> 31)); } conditional conditional - same as x ? y : z Example: conditional(2,4,5) = 4 Legal ops: ! ~ & ^ | + « » Max ops: 16 Rating: 3 1 2 3 4 int conditional(int x, int y, int z) { return ((~(!!x) + 1) & y) | ((~(~(!!x) + 1)) & z); } isLessOrEqual isLessOrEqual - if x <= y then return 1, else return 0 Example: isLessOrEqual(4,5) = 1. Legal ops: ! ~ & ^ | + « » Max ops: 24 Rating: 3 1 2 3 4 5 6 7 8 int isLessOrEqual(int x, int y) { int a = (x >> 31) & 1, b = (y >> 31) & 1; int f = a ^ b; int d = y + ~x + 1; int ds = (d >> 31) & 1; return (f & a) | (!f & !ds); } logicalNeg logicalNeg - implement the ! operator, using all of the legal operators except ! Examples: logicalNeg(3) = 0, logicalNeg(0) = 1, logicalNeg(-5) = 0 Legal ops: ~ & ^ | + « » Max ops: 12 Rating: 4 1 2 3 4 int logicalNeg(int x) { return (((~x + 1) | x) >> 31) + 1; } howManyBits howManyBits - return the minimum number of bits required to represent x in two’s complement Examples: howManyBits(12) = 5 howManyBits(298) = 10 howManyBits(-5) = 4 howManyBits(0) = 1 howManyBits(-1) = 1 howManyBits(0x80000000) = 32 Legal ops: ! ~ & ^ | + « » Max ops: 90 Rating: 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int howManyBits(int x) { int sign = x >> 31, bit16, bit8, bit4, bit2, bit1; x = (sign & ~x) | (~sign & x); bit16 = (!!(x >> 16)) << 4; x >>= bit16; bit8 = (!!(x >> 8)) << 3; x >>= bit8; bit4 = (!!(x >> 4)) << 2; x >>= bit4; bit2 = (!!(x >> 2)) << 1; x >>= bit2; bit1 = (!!(x >> 1)); x >>= bit1; return bit16 + bit2 + bit4 + bit8 + bit1 + x + 1; } floatScale2 floatScale2 - Return bit-level equivalent of expression 2*f for floating point argument f. Both the argument and result are passed as unsigned int’s, but they are to be interpreted as the bit-level representation of single-precision floating point values. When argument is NaN, return argument Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while Max ops: 30 Rating: 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 unsigned floatScale2(unsigned uf) { unsigned sign = (uf >> 31) & 1, exp = (uf >> 23) & 0xff, frac = uf & 0x7fffff; if (exp == 0xff) return uf; if (exp == 0) { if (frac & 0x800000) { exp = 1; } frac <<= 1; return (exp << 23) | (sign << 31) | frac; } exp++; if (exp == 0xff) { return (sign << 31) | (0xff << 23); } return (sign << 31) | (exp << 23) | frac; } floatFloat2Int floatFloat2Int - Return bit-level equivalent of expression (int) f for floating point argument f. Argument is passed as unsigned int, but it is to be interpreted as the bit-level representation of a single-precision floating point value. Anything out of range (including NaN and infinity) should return 0x80000000u. Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while Max ops: 30 Rating: 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 int floatFloat2Int(unsigned uf) { int sign = uf >> 31, exp = ((uf >> 23) & 0xff) - 127, frac = (uf & 0x7fffff) | 0x800000; if (exp >= 31) return 0x80000000u; if (exp < 0) return 0; if (exp < 23) { frac >>= (23 - exp); } else { frac <<= (exp - 23); } if (sign) { frac = -frac; } return frac; } floatPower2 floatPower2 - Return bit-level equivalent of the expression 2.0^x (2.0 raised to the power x) for any 32-bit integer x. The unsigned value that is returned should have the identical bit representation as the single-precision floating-point number 2.0^x. If the result is too small to be represented as a denorm, return 0. If too large, return +INF. Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while Max ops: 30 Rating: 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 unsigned floatPower2(int x) { if (x < -148) { return 0; } if (x < -126) { unsigned f = x + 148; unsigned fra = 1 << f; return fra; } if (x <= 127) { int k = x + 127; return k << 23; } return 0x7f800000; }

September 12, 2025 · 6 min · farmer3-c

latex常用手册

常用符号与表达式 根式:$\sqrt[n]{x}$ 1 \sqrt[n]{x} 分数:$\frac{x}{y}$ 1 \frac{x}{y} 上下标:$x_i^2, a_{ij}^{kl}, \Gamma_{n}^{k}$ 1 x_i^2, a_{ij}^{kl}, \Gamma_{n}^{k} 文本:$\textit{f}_a^b$ 1 \textit{f}_a^b 算子与常见符号:$\nabla, \Delta, \mathrm{i}, \approx, \overline{A}$ 1 \nabla, \Delta, \mathrm{i}, \approx, \overline{A} 希腊字母:$\alpha, \beta, \gamma, \eta, \xi, \phi, \psi, \omega, \theta, \lambda$ 1 \alpha, \beta, \gamma, \eta, \xi, \phi, \psi, \omega, \theta, \lambda 向量与矩阵运算:$\mathbf{a} \times \mathbf{b},\ \mathbf{A + B = C}$ 1 2 \mathbf{a} \times \mathbf{b} \mathbf{A + B = C} 关系与逻辑 不等式:$a \le b,\ a \ge b,\ a \neq b,\ a \ll b,\ a \gg b$ ...

September 6, 2025 · 3 min · farmer3-c